题目 › usaco-1587

Make All Distinct

USACO 2026 Third · 铜组 · 2026-02 · L1 基础 · Python 3.11 · 时限 4 秒 · 测试点等分

查看原题跳到结果

题面

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

You have an integer array $a_1\dots a_N$ with elements initially in the range $[1,N]$ ($1\le N\le 2\cdot 10^5$), as well as a nonzero integer $K$ ($-N \le K\le N, K\neq 0$).

You may perform the following operation as many times as you'd like (possibly zero): select an index $i$ and set $a_i=a_i+K$.

Find the minimum number of operations to make all array elements distinct.

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

The input consists of $T$ ($1\le T\le 10$) independent tests. Each test is described as follows:

The first line contains $N$ and $K$.

The second line contains $a_1\dots a_N$.

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

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

For each test, output a single line containing the minimum number of operations.

Note: The large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).

SAMPLE INPUT:

4
4 1
4 1 4 1
4 -3
4 1 4 1
4 4
4 1 4 1
3 -1
1 1 2

SAMPLE OUTPUT:

2
4
2
1

For the first test, here is a possible sequence of two operations that makes all elements distinct.

4 1 4 1
5 1 4 1 (a_1 += 1)
5 1 4 2 (a_4 += 1)

SCORING:

  • Inputs 2-4: $N\le 50$
  • Inputs 5-7: $N\le 2000$
  • Inputs 8-10: $K=1$
  • Inputs 11-13: No additional constraints.

Problem credits: Akshaj Arora, Benjamin Qi

结果

Kimi K3.1 ProKimi

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 4.5k平均输出 token(含思考) ↓
  • 28.8输出速度 tokens/s ↑
  • 158 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 2Pylint 问题 ↓
  • 9.44Pylint 得分 ↑
  • 9圈复杂度 ↓
  • 61.9可维护性指数 ↑

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

1/1

解出的采样 ↑

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

MiniMax M3.1 FlashMiniMax

auto(max)

1/1

解出的采样 ↑

  • 100采样 1 · 解出
  • 17.0k平均输出 token(含思考) ↓
  • 154.3输出速度 tokens/s ↑
  • 111 秒延迟 ↓
  • —CPU 时间 / 官方解 ↓
  • 无官方解时间复杂度(相对官方解)
  • 3Pylint 问题 ↓
  • 9.21Pylint 得分 ↑
  • 10圈复杂度 ↓
  • 60.6可维护性指数 ↑

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

代码

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


def solve():
    data = sys.stdin.buffer.read().split()
    it = iter(data)
    t = int(next(it))
    answers = []

    for _ in range(t):
        n = int(next(it))
        k = int(next(it))

        # Reflect the values when K is negative, reducing to K > 0.
        sign = 1
        if k < 0:
            k = -k
            sign = -1

        groups = {}
        for _ in range(n):
            x = sign * int(next(it))
            residue = x % k
            if residue in groups:
                groups[residue].append(x)
            else:
                groups[residue] = [x]

        operations = 0

        # Values in different residue classes can never collide.
        for values in groups.values():
            if len(values) < 2:
                continue

            values.sort()
            previous = values[0]

            for i in range(1, len(values)):
                x = values[i]
                if x <= previous:
                    previous += k
                    operations += (previous - x) // k
                else:
                    previous = x

        answers.append(str(operations))

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


if __name__ == "__main__":
    solve()