题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Farmer John has an array $A$ containing $N$ integers ($1 \leq N \leq 5 \cdot 10^5, 1 \leq A_i \leq N$). He picks his favorite index $j$ and take out a sheet of paper with only $A_j$ written on it. He can then perform the following operation some number of times:
- Cyclically shift all elements in $A$ one spot to the left or one spot to the right. Then, write down $A_j$ on a piece of paper.
Let $S$ denote the set of distinct integers that occur in $A$. Farmer John wonders what the minimum number of operations he must perform is so that the paper contains all integers that appear in $S$.
Since it is unclear what FJ's favorite index is, output the answer for all possible favorite indices $1 \leq j \leq N$. Note for each index, $A$ is reset to its original form before performing any operations.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains $N$.The following line contains $A_1, A_2, \ldots, A_N$.
OUTPUT FORMAT (print output to the terminal / stdout):
Output $N$ space-separated integers, where the $i$'th integer is the answer for his favorite index $j = i$.SAMPLE INPUT:
6 1 2 3 1 3 4
SAMPLE OUTPUT:
4 3 3 4 3 3The distinct numbers are $S = \{ 1, 2, 3, 4 \}$. Suppose Farmer John’s favorite index is $j=1$. He starts off with $A_1=1$ written on a piece of paper. We can track the array $A$ after each cyclic shift Farmer John makes.
-
Cyclic shift right: FJ writes down $A_1 = 4$.
4 1 2 3 1 3
-
Cyclic shift left: FJ writes down $A_1 = 1$ again.
1 2 3 1 3 4
-
Cyclic shift left: FJ writes down $A_1 = 2$.
2 3 1 3 4 1
-
Cyclic shift left: FJ writes down $A_1 = 3$.
3 1 3 4 1 2
At this point, Farmer John has written down every number in $S$ using 4 operations.
SAMPLE INPUT:
12 1 1 2 1 1 3 1 1 4 1 1 1
SAMPLE OUTPUT:
8 7 6 7 8 9 8 7 6 7 8 9
SCORING:
- Inputs 3-5: $N\le 500$
- Inputs 6-8: $N\le 10^4$
- Inputs 9-17: No additional constraints.
Problem credits: Chongtian Ma
结果
Kimi K3.1 ProKimi
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 29.7k平均输出 token(含思考) ↓
- 28.3输出速度 tokens/s ↑
- 1052 秒延迟 ↓
- 0.85×CPU 时间 / 官方解 ↓
- 同阶时间复杂度(相对官方解)
- 6Pylint 问题 ↓
- 9.42Pylint 得分 ↑
- 37圈复杂度 ↓
- 45.7可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
0/1
解出的采样 ↑
- 64.7采样 1 · 超时
- 37.7k平均输出 token(含思考) ↓
- 206.8输出速度 tokens/s ↑
- 183 秒延迟 ↓
- 1.19×CPU 时间 / 官方解 ↓
- 同阶时间复杂度(相对官方解)
- 19Pylint 问题 ↓
- 7.71Pylint 得分 ↑
- 26圈复杂度 ↓
- 48.1可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
0/1
解出的采样 ↑
- 5.9采样 1 · 答案错误
- 39.7k平均输出 token(含思考) ↓
- 160.3输出速度 tokens/s ↑
- 248 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- —时间复杂度(相对官方解)
- 3Pylint 问题 ↓
- 9.50Pylint 得分 ↑
- 18圈复杂度 ↓
- 61.4可维护性指数 ↑
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
import sys from collections import deque def forward_ends(a, k): """For each start l, find the shortest complete circular interval [l, r].""" n = len(a) b = a + a count = [0] * (n + 1) ends = [0] * n have = 0 r = 0 for l in range(n): while have < k: v = b[r] r += 1 if count[v] == 0: have += 1 count[v] += 1 ends[l] = r - 1 v = b[l] count[v] -= 1 if count[v] == 0: have -= 1 return ends def solve(): data = sys.stdin.buffer.read().split() n = int(data[0]) a = list(map(int, data[1:1 + n])) del data k = len(set(a)) if k == 1: sys.stdout.write(" ".join(["0"] * n)) return ends = forward_ends(a, k) # Answers using only one direction. ans = [ends[i] - i for i in range(n)] # Enumerate all inclusion-minimal complete intervals. Full-circle intervals # are unnecessary because one-directional answers already cover them. ls = [] rs = [] full_span = n - 1 for l in range(n): r = ends[l] next_r = ends[l + 1] if l + 1 < n else ends[0] + n if r < next_r and r - l < full_span: ls.append(l) rs.append(r) del ends # Repeat in the opposite direction. rev = a[::-1] rev_ends = forward_ends(rev, k) del rev for i in range(n): j = n - 1 - i d = rev_ends[i] - i if d < ans[j]: ans[j] = d del rev_ends m = len(ls) if m: # For an interval [l,r] and a starting coordinate x inside it, the cost # is (r-l) + min(x-l, r-x). # # On the left half this equals x + (r-2l), and on the right half it # equals (2r-l) - x. Sweep coordinates while maintaining the minimum # constant among active intervals with two monotone deques. dq_left = deque() dq_right = deque() add_l = expire_l = 0 add_r = expire_r = 0 inf = 2 * n for x in range(rs[-1] + 1): while add_l < m and ls[add_l] <= x: i = add_l c = rs[i] - 2 * ls[i] while dq_left: b = dq_left[-1] if rs[b] - 2 * ls[b] < c: break dq_left.pop() dq_left.append(i) add_l += 1 while (expire_l < add_l and (ls[expire_l] + rs[expire_l]) // 2 < x): expire_l += 1 while dq_left and dq_left[0] < expire_l: dq_left.popleft() while (add_r < m and (ls[add_r] + rs[add_r] + 1) // 2 <= x): i = add_r c = 2 * rs[i] - ls[i] while dq_right: b = dq_right[-1] if 2 * rs[b] - ls[b] < c: break dq_right.pop() dq_right.append(i) add_r += 1 while expire_r < add_r and rs[expire_r] < x: expire_r += 1 while dq_right and dq_right[0] < expire_r: dq_right.popleft() best = inf if expire_l < add_l: i = dq_left[0] best = x + rs[i] - 2 * ls[i] if expire_r < add_r: i = dq_right[0] value = 2 * rs[i] - ls[i] - x if value < best: best = value j = x if x < n else x - n if best < ans[j]: ans[j] = best sys.stdout.write(" ".join(map(str, ans))) if __name__ == "__main__": solve()
import sys def compute_left(A, K): n = len(A) if K == 1: return [0] * n B = A * 3 M = n << 1 # 2 * n # c[L] = smallest R such that B[L..R] contains all K distinct values c = [0] * M freq = [0] * (n + 1) covered = 0 r = 0 total = 3 * n for L in range(M): while r < total and covered < K: val = B[r] if freq[val] == 0: covered += 1 freq[val] += 1 r += 1 c[L] = r - 1 val = B[L] freq[val] -= 1 if freq[val] == 0: covered -= 1 res = [0] * n # Monotonic queue implemented with a list and head pointer. dq = [] head = 0 # Initial indices needed for s = n: L in [1, n] for idx in range(1, n + 1): v = c[idx] - (idx << 1) while len(dq) > head: last = dq[-1] if c[last] - (last << 1) < v: break dq.pop() dq.append(idx) p = -1 INF = 10**18 for s in range(n, M): if s > n: v = c[s] - (s << 1) while len(dq) > head: last = dq[-1] if c[last] - (last << 1) < v: break dq.pop() dq.append(s) while p + 1 < M and c[p + 1] <= s: p += 1 lower = s - n + 1 left = p + 1 if left < lower: left = lower while head < len(dq) and dq[head] < left: head += 1 best = INF # Case c[L] <= s: best uses the largest such L, namely p. if p >= lower: best = s - (p << 1) # Case c[L] > s: minimize c[L] - 2L. if head < len(dq): idx = dq[head] val2 = c[idx] - (idx << 1) if val2 < best: best = val2 res[s - n] = s + best return res def main(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return n = data[0] A = data[1:1 + n] del data K = len(set(A)) leftA = compute_left(A, K) Arev = A[::-1] leftRev = compute_left(Arev, K) ans = [0] * n for i in range(n): v1 = leftA[i] v2 = leftRev[n - 1 - i] ans[i] = v1 if v1 < v2 else v2 sys.stdout.write(" ".join(map(str, ans)) + "\n") if __name__ == "__main__": main()
import sys from collections import deque def solve(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return n = data[0] a = data[1:n + 1] del data distinct = len(set(a)) doubled = a + a # right_end[left] is the smallest r such that doubled[left:r+1] # contains every distinct value. freq = [0] * (n + 1) have = 0 right = 0 right_end = [0] * n for left in range(n): while have < distinct: value = doubled[right] if freq[value] == 0: have += 1 freq[value] += 1 right += 1 right_end[left] = right - 1 value = doubled[left] freq[value] -= 1 if freq[value] == 0: have -= 1 del a, doubled, freq # The array is periodic, so R[left+n] = R[left] + n. right_end.extend([r + n for r in right_end]) size = 2 * n # For a complete interval [l, R_l], the two possible route costs # at position j are: # j + x[l], where x[l] = R_l - 2*l # y[l] - j, where y[l] = 2*R_l - l x = [0] * size y = [0] * size qx = deque() qy = deque() answer = [""] * n # Evaluate the second copy of the array so circular intervals # can be handled as ordinary linear intervals. for j in range(size): r = right_end[j] xv = r - 2 * j yv = 2 * r - j x[j] = xv y[j] = yv # Remove intervals that no longer contain j. while qx and right_end[qx[0]] < j: qx.popleft() while qy and right_end[qy[0]] < j: qy.popleft() # Maintain increasing values in the two monotonic deques. while qx and x[qx[-1]] >= xv: qx.pop() qx.append(j) while qy and y[qy[-1]] >= yv: qy.pop() qy.append(j) if j >= n: go_left_first = j + x[qx[0]] go_right_first = y[qy[0]] - j best = go_left_first if go_left_first < go_right_first else go_right_first answer[j - n] = str(best) sys.stdout.write(" ".join(answer)) sys.stdout.write("\n") if __name__ == "__main__": solve()