题面
来源: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
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 4.5k平均输出 token(含思考) ↓
- 28.8输出速度 tokens/s ↑
- 158 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 2Pylint 问题 ↓
- 9.44Pylint 得分 ↑
- 9圈复杂度 ↓
- 61.9可维护性指数 ↑
DeepSeek Flash V4.1-1017DeepSeek
1/1
解出的采样 ↑
- 100采样 1 · 解出
- 8.7k平均输出 token(含思考) ↓
- 195.6输出速度 tokens/s ↑
- 45 秒延迟 ↓
- —CPU 时间 / 官方解 ↓
- 无官方解时间复杂度(相对官方解)
- 7Pylint 问题 ↓
- 8.29Pylint 得分 ↑
- 11圈复杂度 ↓
- 47.6可维护性指数 ↑
MiniMax M3.1 FlashMiniMax
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 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
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()
import sys def main(): 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)) d = abs(K) groups = {} if K > 0: for _ in range(N): v = int(next(it)) q, r = divmod(v, d) if r in groups: groups[r].append(q) else: groups[r] = [q] else: for _ in range(N): v = int(next(it)) c = -v q, r = divmod(c, d) if r in groups: groups[r].append(q) else: groups[r] = [q] ans = 0 for arr in groups.values(): if len(arr) <= 1: continue arr.sort() last = arr[0] for i in range(1, len(arr)): x = arr[i] if x > last: last = x else: last += 1 ans += last - x answers.append(str(ans)) sys.stdout.write("\n".join(answers)) if __name__ == "__main__": main()
import sys def solve(): data = list(map(int, sys.stdin.buffer.read().split())) if not data: return t = data[0] pos = 1 results = [] for _ in range(t): n = data[pos] k = data[pos + 1] pos += 2 d = abs(k) direction = 1 if k > 0 else -1 # Elements with different remainders modulo d cannot collide. groups = [[] for _ in range(d)] end = pos + n for i in range(pos, end): value = data[i] remainder = value % d coordinate = direction * (value // d) groups[remainder].append(coordinate) pos = end answer = 0 for coordinates in groups: if len(coordinates) <= 1: continue coordinates.sort() # The previous target coordinate; one operation increases it by 1. last = coordinates[0] - 1 for initial in coordinates: if initial > last: target = initial else: target = last + 1 answer += target - initial last = target results.append(str(answer)) sys.stdout.write("\n".join(results)) if __name__ == "__main__": solve()