Codeforces Round 1066 (Div. 1 + Div. 2)
E
Adjusting Drones
Codeforces
1900
原题
← DF →
翻译中…
翻译中…

You are managing a fleet of $n$ drones, each with an energy level $a_1, \ldots, a_n$. You are also given a positive integer $k$, which represents the maximum number of drones allowed to share the same energy level.

翻译中…

To prevent overloads, the drones automatically perform energy balancing operations as follows. While there exists an energy level that appears strictly more than $k$ times, they execute the following steps:

翻译中…
  • first, every drone $i$ whose energy $a_i$ appears earlier (that is, there exists $j \lt i$ with $a_j = a_i$) is marked;
  • then, for each marked drone, its energy is increased by $1$ unit;
  • then, the marks are removed.
翻译中…

The process stops once no energy level appears more than $k$ times. Determine how many energy balancing operations will be performed.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$, $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$) — the number of drones and the maximum allowed number of drones with the same energy level.

翻译中…

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 2n$) — the initial energy levels of the drones.

翻译中…

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

翻译中…

Output

翻译中…

For each test case, output a single line containing an integer: the number of energy balancing operations performed.

翻译中…

Note

翻译中…

In the first test case, the drones' energy levels evolve as follows:

翻译中…
  • at the beginning, $[1, 1, 1, 1, 1, 1]$;
  • after $1$ operation, $[1, 2, 2, 2, 2, 2]$;
  • after $2$ operations, $[1, 2, 3, 3, 3, 3]$;
  • after $3$ operations, $[1, 2, 3, 4, 4, 4]$.
翻译中…

After $3$ operations, every energy level appears at most $3$ times, so the process stops.

翻译中…

In the second test case, the energy levels change as follows: $[1, 3, 2, 1, 4] \rightarrow [1, 3, 2, 2, 4] \rightarrow [1, 3, 2, 3, 4] \rightarrow [1, 3, 2, 4, 4] \rightarrow [1, 3, 2, 4, 5]$. The process stops after $4$ operations.

翻译中…

样例

Input #1
5
6 3
1 1 1 1 1 1
5 1
1 3 2 1 4
6 2
1 1 1 2 3 3
4 1
8 8 8 8
2 2
1 2
Output #1
3
4
4
3
0
InputCopy OutputCopy
题目信息
题号
2157E
时间限制
2 s
内存限制
256 MB
标签
二分
暴力
数据结构
动态规划
并查集
贪心
模拟
排序

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。