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.
Try looking at the partition with just one multiset: the whole array itself. That already gives you one valid partition.
If every part has MEX , then no part can contain . So where could an occurrence of from go?
Also, if every part has MEX , then every part must contain all numbers .
So a valid score means: is absent from the whole multiset, and every smaller non-negative integer appears in the whole multiset.
That condition is exactly the definition of . The whole partition drama is a decoy. Just compute the MEX of the array.
The key is to not get baited by the word partition. It looks like you need to split the multiset cleverly, but the answer is actually forced.
Suppose some valid partition has score . That means every multiset in the partition has MEX .
For a multiset to have MEX :
Now apply that to every part of the partition.
Since no part may contain , the entire original multiset cannot contain either. There is nowhere to put it. If appeared in , some part would have to receive it, and then that part's MEX would not be . Instant contradiction.
Also, since each part contains all numbers smaller than , the original multiset definitely contains all numbers .
So any valid score must satisfy:
and
But that is literally the definition of .
So every valid partition has score exactly .
And we know this score is achievable, because we can use the dumbest possible partition: just one multiset, itself. Its MEX is , so the partition is valid.
Therefore the minimum score is simply:
No splitting. No optimization. The problem put on a fake mustache and called itself partitioning.
Algorithm
For each test case:
Since and values are at most , checking up to about is enough.
Complexity
For each test case, the time complexity is where is the small value range, around .
Memory usage is .
#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<bool> seen(105, false);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
seen[x] = true;
}
int mex = 0;
while (seen[mex]) mex++;
cout << 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;
cin >> n;
vector<bool> seen(105, false);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
seen[x] = true;
}
int mex = 0;
while (seen[mex]) mex++;
cout << mex << '\n';
}
return 0;
}