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.
Fix a candidate MEX value . Instead of thinking about all deletion choices, ask: what must be true in the remaining array for the MEX to be exactly ?
For MEX , every number must still appear at least once, and every copy of must be deleted. So if some value below is missing in the original array, is dead on arrival.
The minimum number of deletions needed for MEX is , because all copies of must go. The maximum number is , because you must leave at least one copy of each of the smaller values.
If all values below exist, then every in the interval is achievable. Extra deletions can come from duplicates below or from values above .
So each possible MEX contributes to a whole range of answers. Use a difference array: add at and after , then prefix-sum it to get the answer for every .
For each deletion count , we need the number of distinct MEX values that can happen after deleting exactly elements.
The trap is trying to simulate deletions. Don't. That's a combinatorial swamp, and it smells bad.
Instead, fix the MEX value first.
When is MEX possible?
After deletions, having means:
So two things matter:
Let be the frequency of value .
The minimum deletions needed for MEX is clearly:
because every copy of has to disappear.
Now, what is the maximum number of deletions?
We must keep at least one copy of each value . That's elements that must remain. Everything else can be deleted.
So the maximum deletions is:
Therefore, if all values below exist, MEX is possible for exactly these deletion counts:
provided the interval is not empty.
Why every value inside the interval works
After deleting all copies of , we may still need to delete more elements.
The optional deletions can come from:
The total number of optional removable elements is exactly:
So we can delete any extra amount from up to that number. No weird gaps. No hidden parity nonsense. Just a clean interval.
Counting intervals
Now each valid MEX contributes to every answer where:
This screams difference array.
For each valid :
diff[cnt[m]]++diff[n - m + 1]--Then prefix-sum diff to get all answers.
Which values do we check?
We scan to while keeping whether all smaller values have appeared.
For , the condition is vacuously true.
After processing , if , then any larger MEX becomes impossible, because that larger MEX would require to be present. So the scan can stop after that missing value.
Complexity per test case:
Total complexity over all tests:
which is easily fine.
#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;
cin >> n;
vector<int> cnt(n + 1, 0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
cnt[x]++;
}
vector<int> diff(n + 2, 0);
for (int mex = 0; mex <= n; mex++) {
int l = cnt[mex];
int r = n - mex;
if (l <= r) {
diff[l]++;
diff[r + 1]--;
}
if (cnt[mex] == 0) break;
}
int cur = 0;
for (int k = 0; k <= n; k++) {
cur += diff[k];
cout << cur << (k == n ? '\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;
cin >> n;
vector<int> cnt(n + 1, 0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
cnt[x]++;
}
vector<int> diff(n + 2, 0);
for (int mex = 0; mex <= n; mex++) {
int l = cnt[mex];
int r = n - mex;
if (l <= r) {
diff[l]++;
diff[r + 1]--;
}
if (cnt[mex] == 0) break;
}
int cur = 0;
for (int k = 0; k <= n; k++) {
cur += diff[k];
cout << cur << (k == n ? '\n' : ' ');
}
}
return 0;
}