QOJ.ac

QOJ

Límite de tiempo: 2.0 s Límite de memoria: 1024 MB Puntuación total: 100 Hackeable ✓

#15708. 调整无人机

Estadísticas

你正在管理由 $n$ 架无人机组成的机群,每架无人机的能量水平分别为 $a_1, \dots, a_n$。同时给定一个正整数 $k$,表示允许拥有相同能量水平的无人机的最大数量。

为了防止过载,无人机会自动执行能量平衡操作。只要存在某种能量水平的出现次数严格大于 $k$,它们就会循环执行以下步骤:

  • 首先,标记所有能量 $a_i$ 在之前已经出现过的无人机 $i$(即存在 $j < i$ 满足 $a_j = a_i$);
  • 然后,对于每架被标记的无人机,其能量增加 $1$ 个单位;
  • 最后,清除所有标记。

当没有任意一种能量水平出现超过 $k$ 次时,该过程停止。请计算将执行多少次能量平衡操作。

输入格式

每个测试点包含多个测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 10^4$)。接下来是测试用例的描述。

每个测试用例的第一行包含两个整数 $n, k$($1 \le k \le n \le 2 \cdot 10^5$)—— 无人机的数量以及允许拥有相同能量水平的无人机的最大数量。

每个测试用例的第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$($1 \le a_i \le 2n$)—— 无人机的初始能量水平。

保证所有测试用例中 $n$ 的总和不超过 $2 \cdot 10^5$。

输出格式

对于每个测试用例,输出一行,包含一个整数:执行的能量平衡操作次数。

样例

输入 1

5
6 3
1 1 1 1 1 1
5 1
1 3 2 1 4
16 2
6 3 7 5 13 6 7 6 3 5 13 7 6 4 3 6
16 3
6 3 7 5 13 6 7 6 3 5 13 7 6 4 3 6
16 4
6 3 7 5 13 6 7 6 3 5 13 7 6 4 3 6

输出 1

3
4
14
5
4

说明

样例 1 解释:在第一个测试用例中,无人机的能量水平变化如下:

  • 初始时:$[1, 1, 1, 1, 1, 1]$;
  • $1$ 次操作后:$[1, 2, 2, 2, 2, 2]$;
  • $2$ 次操作后:$[1, 2, 3, 3, 3, 3]$;
  • $3$ 次操作后:$[1, 2, 3, 4, 4, 4]$。

在 $3$ 次操作后,每个能量水平最多出现 $3$ 次,因此过程停止。

在第二个测试用例中,能量水平变化如下: $[1, 3, 2, 1, 4] \to [1, 3, 2, 2, 4] \to [1, 3, 2, 3, 4] \to [1, 3, 2, 4, 4] \to [1, 3, 2, 4, 5]$。该过程在 $4$ 次操作后停止。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.