CF2183B.Yet Another MEX Problem

普及-

通过率:0%

时间限制:1.50s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 n−k+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 max⁡i=1∣a∣−k+1f(i,i+k−1)=f(l,r).\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 l≤i≤rl \leq i \leq r, and delete aia_i from aa. That is, your new sequence will be [a1,a2,…,ai−1,ai+1,ai+2,…,an][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 mex⁡3\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 n−k+1n - k + 1 operations, you will have a sequence of length k−1k-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.

给你一个长度为 nn 的数组 aa 和一个整数 kk。定义 f(l,r)f(l,r) 为子数组 al,al+1,…,ara_l,a_{l+1},\ldots,a_r 的 mex⁡\operatorname{mex} 值∗^{\text{∗}}。你需要执行以下操作 n−k+1n-k+1 次:

  • 设当前序列长度为 ∣a∣|a|。你需要找出一个长度为 kk 的区间 [l,r][l, r],使得

    max⁡i=1∣a∣−k+1f(i,i+k−1)=f(l,r).\operatorname{max}_{i=1}^{|a|-k+1} f(i, i+k-1) = f(l, r).

    换言之,你需要选择一个大小为 kk 的滑动窗口,使其 mex⁡\operatorname{mex} 在所有大小为 kk 的窗口中达到最大。若存在多个满足条件的 [l,r][l,r],任选其一即可。接着,你必须任选一个下标 ii 满足 l≤i≤rl \leq i \leq r,并将 aia_i 从 aa 中删除。即新序列为 [a1,a2,…,ai−1,ai+1,ai+2,…,an][a_1, a_2, \ldots, a_{i-1}, a_{i+1}, a_{i+2}, \ldots, a_n]。

例如,在数组 [1,2,0,1,3,0][1,2,0,1,3,0] 中取 k=3k=3,所有长度为 33 的窗口中,(l,r)=(1,3)(l,r) = (1,3) 和 (2,4)(2,4) 对应的 mex⁡\operatorname{mex} 均为 33(这是所有长度为 33 的窗口中 mex⁡\operatorname{mex} 的最大值)。因此,下一步你可以删除下标 1,2,31,2,3 或 44 中的任意一个元素。

经过 n−k+1n - k + 1 次操作后,剩余序列的长度将为 k−1k-1。你的目标是使剩余元素的 mex⁡\operatorname{mex} 尽可能大。请输出所能达到的最大 mex⁡\operatorname{mex} 值。

∗^{\text{∗}} 一组整数 c1,c2,…,ckc_1, c_2, \ldots, c_k 的最小未出现值(MEX) 定义为未在该集合中出现的最小非负整数 xx。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \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 (2≤k≤n≤2⋅1052 \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 (0≤ai≤n0 \le a_i \le n).

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

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个正整数 nn 和 kk(2≤k≤n≤2⋅1052 \le k \le n \le 2 \cdot 10^5)。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1,a_2,\ldots,a_n(0≤ai≤n0 \le a_i \le n)。

保证所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

Output a single non-negative integer representing your answer.

输出一个表示答案的非负整数。

输入输出样例

  • 输入#1

    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
    1
    2
    1
    4

说明/提示

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.

在第一个测试用例中,我们可以选择区间 [1,3][1,3]。然后删除 a2a_2,得到 [0,0][0,0]。该过程终止,剩余元素的 mex⁡\operatorname{mex} 为 11。

在第三个测试用例中,我们可以执行以下操作:

  • 选择 i=1,j=3i=1,j=3。这是允许的,因为所有长度为 33 的滑动窗口的 mex⁡\operatorname{mex} 值分别为 3,0,33,0,3,而窗口 [1,3][1,3] 的 mex⁡\operatorname{mex} 为 33,是其中最大的。随后删除 a2a_2,序列变为 [0,2,1,0][0,2,1,0]。
  • 选择 i=2,j=4i=2,j=4。然后删除 a2a_2,序列变为 [0,1,0][0,1,0]。
  • 选择 i=1,j=3i=1,j=3。然后删除 a1a_1,序列变为 [1,0][1,0]。该过程终止,最终的 mex⁡\operatorname{mex} 为 22。

输入解题思路,AI测评打分。不知道怎么写?

首页