题目 › usaco-1494

The Best Lineup

USACO 2025 February · 银组 · 2025-02 · L2 中等 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字。

Farmer John has $N$ $(1 \leq N \leq 2 \cdot 10^5)$ cows in a line $a$. The $i$'th cow from the front of line $a$ is labeled an integer $a_i$ ($1 \leq a_i \leq N$). Multiple cows may be labeled the same integer.

FJ will construct another line $b$ in the following manner:

  • Initially, $b$ is empty.
  • While $a$ is nonempty, remove the cow at the front of $a$ and potentially add that cow to the back of $b$.

FJ wants to construct line $b$ such that the sequence of labels in $b$ from front to back is lexicographically greatest (see the footnote).

Before FJ constructs line $b$, he can perform the following operation at most once:

  • Choose a cow in line $a$ and move it anywhere before its current position.

Given that FJ optimally performs the aforementioned operation at most once, output the lexicographically greatest label sequence of $b$ he can achieve.

Each input will consist of $T$ ($1 \leq T \leq 100$) independent test cases.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains $T$.

The first line of each test case contains $N$.

The second line of each test case contains $N$ space-separated integers $a_1, a_2, \ldots, a_N$.

It is guaranteed that the sum of $N$ over all test cases does not exceed $10^6$.

OUTPUT FORMAT (print output to the terminal / stdout):

For each test case, output the lexicographically greatest $b$ on a new line.

SAMPLE INPUT:

3
5
4 3 2 1 3
6
5 1 2 6 3 4
6
4 1 3 2 1 1

SAMPLE OUTPUT:

4 3 3 2 1
6 5 4
4 3 2 1 1

In the first test case, FJ can move the fifth cow to directly after the second cow. Now, $a = [4, 3, 3, 2, 1]$. It can be shown $[4, 3, 3, 2, 1]$ is also the lexicographically greatest $b$.

In the second test case, FJ can move the fourth cow to the front of the line.

In the third test case, FJ does not need to perform any operations. He can construct $b$ by adding each cow beside the second cow to the back of $b$. It can be shown this results in the lexicographically greatest $b$.

SCORING:

  • Inputs 2-4: $N \leq 100$
  • Inputs 5-8: $N \leq 750$
  • Inputs 9-18: No additional constraints

FOOTNOTE:

Recall that a sequence $s$ is lexicographically greater than a sequence $t$ if and only if one of the following holds:
  • At the first position $i$ where $s_i \neq t_i$, $s_i > t_i$.
  • If no such $i$ exists, $s$ is longer than $t$.

Problem credits: Chongtian Ma, Haokai Ma, Andrew Li

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 26.0k平均输出 token(含思考) ↓
  • 25.8输出速度 tokens/s ↑
  • 1009 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 4Pylint 问题 ↓
  • 9.27Pylint 得分 ↑
  • 17圈复杂度 ↓
  • 55.9可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 42.5k平均输出 token(含思考) ↓
  • 195.2输出速度 tokens/s ↑
  • 219 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 8Pylint 问题 ↓
  • 8.75Pylint 得分 ↑
  • 19圈复杂度 ↓
  • 58.8可维护性指数 ↑

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 52.4k平均输出 token(含思考) ↓
  • 132.4输出速度 tokens/s ↑
  • 397 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 7Pylint 问题 ↓
  • 9.09Pylint 得分 ↑
  • 29圈复杂度 ↓
  • 54.8可维护性指数 ↑

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 83 行
import sys
from bisect import bisect_left


def solve():
    data = list(map(int, sys.stdin.buffer.read().split()))
    if not data:
        return

    t = data[0]
    ptr = 1
    answers = []

    for _ in range(t):
        n = data[ptr]
        ptr += 1
        a = data[ptr:ptr + n]
        ptr += n

        positions = [[] for _ in range(n + 1)]
        for i, value in enumerate(a, 1):
            positions[value].append(i)

        # suffix_max[i] = maximum of a[i..n], using 1-based positions.
        suffix_max = [0] * (n + 2)
        for i in range(n - 1, -1, -1):
            suffix_max[i + 1] = max(a[i], suffix_max[i + 2])

        result = []
        start = 1

        while True:
            maximum = suffix_max[start]
            occurrence_positions = positions[maximum]
            last = occurrence_positions[-1]

            count = len(occurrence_positions) - bisect_left(
                occurrence_positions, start
            )
            result.extend([maximum] * count)

            if count > 1:
                previous = occurrence_positions[-2]
            else:
                previous = start - 1

            # Maximum strictly between the previous and last occurrences.
            between_max = 0
            for j in range(previous, last - 1):
                if a[j] > between_max:
                    between_max = a[j]

            next_max = suffix_max[last + 1]

            if between_max >= next_max:
                # Append the lexicographically greatest subsequence of
                # a[previous+1..last-1] + a[last+1..n].
                stack = []

                for j in range(previous, last - 1):
                    value = a[j]
                    while stack and stack[-1] < value:
                        stack.pop()
                    stack.append(value)

                for j in range(last, n):
                    value = a[j]
                    while stack and stack[-1] < value:
                        stack.pop()
                    stack.append(value)

                result.extend(stack)
                break

            start = last + 1

        answers.append(" ".join(map(str, result)))

    sys.stdout.write("\n".join(answers))


if __name__ == "__main__":
    solve()