题面
来源: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
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 26.0k平均输出 token(含思考) ↓
- 25.8输出速度 tokens/s ↑
- 1009 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 4Pylint 问题 ↓
- 9.27Pylint 得分 ↑
- 17圈复杂度 ↓
- 55.9可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 42.5k平均输出 token(含思考) ↓
- 195.2输出速度 tokens/s ↑
- 219 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 8Pylint 问题 ↓
- 8.75Pylint 得分 ↑
- 19圈复杂度 ↓
- 58.8可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
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 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
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()
import sys def solve() -> None: data = list(map(int, sys.stdin.buffer.read().split())) it = iter(data) t = next(it) outputs = [] for _ in range(t): n = next(it) a = [next(it) for _ in range(n)] # S is the lexicographically greatest subsequence of a with no move. # It consists exactly of the suffix maxima of a, left to right. s_pos = [] s_val = [] cur_max = -1 for i in range(n - 1, -1, -1): if a[i] >= cur_max: s_pos.append(i) s_val.append(a[i]) cur_max = a[i] s_pos.reverse() s_val.reverse() ns = len(s_val) found = False chosen_k = -1 chosen_L = -1 chosen_p = -1 j = 0 while j < ns: k = j val = s_val[j] # Find the end of this block of equal values. while k + 1 < ns and s_val[k + 1] == val: k += 1 p = s_pos[k] L = s_pos[k - 1] if k > 0 else -1 tail_max = s_val[k + 1] if k + 1 < ns else -1 gap_max = -1 for idx in range(L + 1, p): if a[idx] > gap_max: gap_max = a[idx] # Moving the last occurrence of val can improve the tail iff # the gap before it contains a value >= the next suffix maximum. if L + 1 < p and gap_max >= tail_max: found = True chosen_k = k chosen_L = L chosen_p = p break j = k + 1 if not found: outputs.append(" ".join(map(str, s_val))) continue # Prefix is the original S through the whole chosen value block. ans = s_val[:chosen_k + 1] # Build the improved tail C = LMS(a[chosen_L+1 : chosen_p] + a[chosen_p+1 :]). rev_c = [] cur_max = -1 # Process suffix after chosen_p. for idx in range(n - 1, chosen_p, -1): if a[idx] >= cur_max: rev_c.append(a[idx]) cur_max = a[idx] # Process gap before chosen_p. for idx in range(chosen_p - 1, chosen_L, -1): if a[idx] >= cur_max: rev_c.append(a[idx]) cur_max = a[idx] c = rev_c[::-1] ans.extend(c) outputs.append(" ".join(map(str, ans))) sys.stdout.write("\n".join(outputs)) if __name__ == "__main__": solve()
import sys def solve_case(a): n = len(a) # next_greater[i]: nearest j > i with a[j] > a[i], or -1. next_greater = [-1] * n # max_after[i]: maximum label strictly after position i. max_after = [0] * n stack = [] root_positions = [] right_max = 0 for i in range(n - 1, -1, -1): max_after[i] = right_max x = a[i] if x > right_max: right_max = x # Equal labels do not dominate each other, so pop <= x. while stack and a[stack[-1]] <= x: stack.pop() if stack: next_greater[i] = stack[-1] else: root_positions.append(i) stack.append(i) # gain_events[v] contains the candidate roots whose move adds # one occurrence of label v to the original answer. gain_events = [None] * (n + 1) active = set() for i, x in enumerate(a): j = next_greater[i] # Only suffix-maximum cows are useful candidates. if j == -1 or next_greater[j] != -1: continue # There must be no larger cow after j. if x < max_after[j]: continue bucket = gain_events[x] if bucket is None: gain_events[x] = [j] else: bucket.append(j) active.add(j) # Compare candidates by their gain-count vectors, from high label to low. for value in range(n, 0, -1): bucket = gain_events[value] if bucket is None: continue counts = {} for candidate in bucket: if candidate in active: counts[candidate] = counts.get(candidate, 0) + 1 if not counts: continue largest_count = max(counts.values()) active = { candidate for candidate, count in counts.items() if count == largest_count } if len(active) == 1: break # Count the labels in the original optimal subsequence. frequency = [0] * (n + 1) for i in root_positions: frequency[a[i]] += 1 # Add all gains from the best candidate. if active: chosen = next(iter(active)) threshold = max_after[chosen] for i, x in enumerate(a): if next_greater[i] == chosen and x >= threshold: frequency[x] += 1 # The answer is nonincreasing. answer = [] for value in range(n, 0, -1): count = frequency[value] if count: answer.extend([str(value)] * count) return " ".join(answer) def solve(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return t = data[0] pos = 1 output = [] for _ in range(t): n = data[pos] pos += 1 a = data[pos:pos + n] pos += n output.append(solve_case(a)) sys.stdout.write("\n".join(output) + "\n") if __name__ == "__main__": solve()