CF2183B.Yet Another MEX Problem

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Yet Another MEX Problem

You are given an array aa of length nn, and an integer kk. Let f(l,r)f(l,r) be the value of mex(al,al+1,,ar)\operatorname{mex}(a_l,a_{l+1},\ldots,a_r)^{\text{∗}}. You want to perform the following operation nk+1n-k+1 times:

  • Let the current length of the sequence be a|a|. You need to find an interval [l,r][l, r] of length kk such that $\operatorname{max}_{i=1}^{|a|-k+1} f(i, i+k-1) = f(l, r).$ In other words, you need to select a window of size kk such that among all windows of size kk, the window you selected has the maximum mex\operatorname{mex}. If multiple [l,r][l,r] exist, you may select any. Then, you must select any integer ii such that lirl \leq i \leq r, and delete aia_i from aa. That is, your new sequence will be $[a_1, a_2, \ldots, a_{i-1}, a_{i+1}, a_{i+2}, \ldots, a_n]$.

For example, in the array [1,2,0,1,3,0][1,2,0,1,3,0] with k=3k=3, the two possible (l,r)(l,r) pairs are (1,3)(1,3) and (2,4)(2,4) (since they each have mex3\operatorname{mex} 3, which is the maximum among all windows of size 33). Therefore, you can remove any one of the indices 1,2,3,41,2,3,4 in your next move.

After nk+1n - k + 1 operations, you will have a sequence of length k1k-1. Your objective is to maximize the mex\operatorname{mex} of the remaining elements. Please output the maximum mex\operatorname{mex} possible.

^{\text{∗}}The minimum excluded (MEX) of a collection of integers c1,c2,,ckc_1, c_2, \ldots, c_k is defined as the smallest non-negative integer xx which does not occur in the collection cc.

Input

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

The first line of each test case contains two positive integers nn and kk (2kn21052 \le k \le n \le 2 \cdot 10^5).

The second line of each test case contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n (0ain0 \le a_i \le n).

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

Output

Output a single non-negative integer representing your answer.

Note

In the first test case, we can select the interval [1,3][1,3]. Then, delete a2a_2 to obtain [0,0][0,0]. The process terminates, and the mex\operatorname{mex} of the remaining elements is 11.

In the third test case, we can perform the following operations:

  • Select i=1,j=3i=1,j=3. This is allowed because the mex\operatorname{mex} of all windows of size 33 is 3,0,33,0,3, and the window [1,3][1,3] has a mex\operatorname{mex} of 33, which is the largest. Then, remove a2a_2. The sequence is now [0,2,1,0][0,2,1,0].
  • Select i=2,j=4i=2,j=4. Then, remove a2a_2. The sequence is now [0,1,0][0,1,0].
  • Select i=1,j=3i=1,j=3. Then, remove a1a_1. The sequence is now [1,0][1,0]. The process terminates, and the final mex\operatorname{mex} is 22.

Samples

5
3 3
0 0 0
4 2
0 2 1 3
5 3
0 1 2 1 0
6 2
0 1 0 1 2 0
7 5
0 1 2 4 0 3 1
1
1
2
1
4

在线编程 IDE

建议全屏模式获得最佳体验