题目 › usaco-1568

Farmer John Loves Rotations

USACO 2026 Second · 银组 · 2026-01 · L2 中等 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

来源: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 3
The 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.

  1. Cyclic shift right: FJ writes down $A_1 = 4$.
     4 1 2 3 1 3 
  2. Cyclic shift left: FJ writes down $A_1 = 1$ again.
     1 2 3 1 3 4 
  3. Cyclic shift left: FJ writes down $A_1 = 2$.
     2 3 1 3 4 1 
  4. 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

auto(max)

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

auto(high)

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

auto(max)

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 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 139 行
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()