题目 › usaco-1471

Farmer John's Favorite Operation

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

查看原题跳到结果

题面

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

It is another cold and boring day on Farmer John's farm. To pass the time, Farmer John has invented a fun leisure activity involving performing operations on an integer array.

Farmer John has an array $a$ of $N$ ($1 \leq N \leq 2 \cdot 10^5$) non-negative integers and an integer $M$ ($1 \leq M \leq 10^9$). Then, FJ will ask Bessie for an integer $x$. In one operation, FJ can pick an index $i$ and subtract or add $1$ to $a_i$. FJ's boredom value is the minimum number of operations he must perform so that $a_i-x$ is divisible by $M$ for all $1 \leq i \leq N$.

Among all possible $x$, output FJ's minimum possible boredom value.

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

The first line contains $T$ ($1 \leq T \leq 10$), the number of independent test cases to solve.

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

The second line of each test case contains $a_1, a_2, ..., a_N$ ($0 \leq a_i \leq 10^9$).

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

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

For each test case, output an integer on a new line containing FJ's minimum possible boredom value among all possible values of $x$.

SAMPLE INPUT:

2
5 9
15 12 18 3 8
3 69
1 988244353 998244853

SAMPLE OUTPUT:

10
21

In the first test case, one optimal choice of $x$ is $3$. FJ can perform $10$ operations to make $a = [12, 12, 21, 3, 12]$.

SCORING:

  • Input 2: $N \le 1000$ and $M \le 1000$.
  • Input 3: $N\le 1000$.
  • Inputs 4-5: $M\le 10^5$.
  • Inputs 6-16: No additional constraints.

Problem credits: Chongtian Ma

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 8.3k平均输出 token(含思考) ↓
  • 28.9输出速度 tokens/s ↑
  • 288 秒延迟 ↓
  • 1.01×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 3Pylint 问题 ↓
  • 9.12Pylint 得分 ↑
  • 9圈复杂度 ↓
  • 66.6可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 24.2k平均输出 token(含思考) ↓
  • 220.7输出速度 tokens/s ↑
  • 110 秒延迟 ↓
  • 1.97×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 7Pylint 问题 ↓
  • 9.22Pylint 得分 ↑
  • 25圈复杂度 ↓
  • 49.0可维护性指数 ↑

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 17.1k平均输出 token(含思考) ↓
  • 191.3输出速度 tokens/s ↑
  • 90 秒延迟 ↓
  • 0.97×CPU 时间 / 官方解 ↓
  • 同阶时间复杂度(相对官方解)
  • 4Pylint 问题 ↓
  • 9.05Pylint 得分 ↑
  • 12圈复杂度 ↓
  • 61.8可维护性指数 ↑

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

代码

Kimi K3.1 Pro · 采样 1 · 53 行
import sys
from bisect import bisect_right


def minimum_boredom(residues, m):
    residues.sort()
    n = len(residues)

    # Two copies allow every cyclic distance to be treated as a forward distance.
    extended = residues + [r + m for r in residues]
    prefix = [0]
    for value in extended:
        prefix.append(prefix[-1] + value)

    half = m // 2
    answer = None

    # An optimal x can always be chosen congruent to one of the array elements.
    for i, x in enumerate(residues):
        end = i + n
        split = bisect_right(extended, x + half, i, end)

        # Points at forward distance at most floor(M/2).
        count = split - i
        cost = prefix[split] - prefix[i] - count * x

        # Remaining points are closer in the backward direction.
        count = end - split
        cost += count * (m + x) - (prefix[end] - prefix[split])

        if answer is None or cost < answer:
            answer = cost

    return answer


def solve():
    data = sys.stdin.buffer.read().split()
    iterator = iter(data)
    test_cases = int(next(iterator))
    output = []

    for _ in range(test_cases):
        n = int(next(iterator))
        m = int(next(iterator))
        residues = [int(next(iterator)) % m for _ in range(n)]
        output.append(str(minimum_boredom(residues, m)))

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


if __name__ == "__main__":
    solve()