Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Forget the exact deletion order for a second. The final array has length , so its MEX can never exceed . That upper bound is boring but important.
To end with MEX at least , the final elements must contain every value . Since deletions never create values, all those values must already appear in the original array.
Now ask the annoying part: can the operation rule stop us from preserving one chosen occurrence of each needed value? The rule only says the deleted element must lie inside a currently maximum-MEX window, but you may delete any element in that window.
If the array still has at least elements and you are trying to preserve a set of at most positions, then no length- window can be fully contained in . So every chosen window has at least one disposable element.
Therefore any fixed subsequence of length can be kept until the end: whenever a maximum-MEX window is chosen, delete some element in it that is not in your target subsequence. So the answer is just the largest such that all values appear in the original array.
Key observation
The operation sounds scarier than it is. Classic Codeforces move: wrap a simple invariant in enough words to make your brain file a complaint.
After all deletions, exactly elements remain. So immediately:
because having MEX would require the final array to contain all values , which needs at least elements.
Also, deletions do not create values. So if we want final MEX at least , the original array must contain every value:
This gives an upper bound: the answer is at most the MEX of the whole original array, and at most .
So:
Now we need to prove this bound is actually achievable.
Why the deletion rule does not hurt us
Suppose we want to preserve some target subsequence of exactly elements. We are allowed to choose which actual occurrences to preserve.
During the process, while the current array length is at least , the operation selects some maximum-MEX window of length . We do not control which maximum window exists, but we are allowed to pick any of them, and after choosing one, we may delete any element inside that window.
Here is the important invariant:
As long as the target subsequence has size , no length- window can consist only of target elements.
That is just counting. A window has elements, but there are only protected elements total. So every length- window contains at least one unprotected element.
Therefore, whenever the operation forces us to delete from a maximum-MEX window, that window contains some disposable element. Delete that one. The protected elements survive.
Repeat until only elements remain. Since we never deleted protected elements, the remaining array is exactly our chosen target subsequence.
So yes: any subsequence of length can be made the final array. The maximum-MEX-window rule looks dangerous, but it cannot trap us. It is all bark, no bite.
Choosing the best target subsequence
Let
By definition of , every value appears in the original array.
Pick one occurrence of each of those values. That gives us protected elements. Since , we can add any other arbitrary elements until the protected set has exactly positions.
By the argument above, we can preserve those elements until the end. The final array contains all values , so its MEX is at least .
The upper bound already said the answer cannot exceed , so the answer is exactly:
Algorithm
For each test case:
Values satisfy , so checking presence up to is enough.
Complexity
For each test case, the work is .
Across all test cases, this is:
which is easily fine for .
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> seen(n + 2, 0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x <= n + 1) seen[x] = 1;
}
int mex = 0;
while (seen[mex]) mex++;
cout << min(k - 1, mex) << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> seen(n + 2, 0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x <= n + 1) seen[x] = 1;
}
int mex = 0;
while (seen[mex]) mex++;
cout << min(k - 1, mex) << '\n';
}
return 0;
}