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.
Look at the value first. If a segment has no , its MEX is immediately .
If the array has no zeroes, every nonempty prefix and suffix has MEX . That is an instant NO.
If there is exactly one zero, no split can put a zero on both sides. One side has MEX , and the other side has MEX at least .
The only scary case is multiple zeroes. If there is no , then any split between the first and last zero gives both sides MEX .
If there is at least one and at least one , put all zeroes first. Splits inside the zero block have prefix MEX and suffix MEX at least ; later splits have suffix MEX .
The whole problem is basically about whether we can stop both sides from having the same relationship with and .
Start with the most important fact: if a segment does not contain , its MEX is . No ceremony, no hidden trick.
So if the array has no zeroes, then every nonempty prefix and every nonempty suffix has MEX . Since , there is at least one split, and that split is bad. Therefore:
Now suppose there is exactly one zero. For any split, that single zero is on exactly one side. The side without the zero has MEX . The side with the zero has MEX at least . So the two MEX values are always different. Therefore:
Now suppose there are at least two zeroes. This is where a tempting but wrong thought shows up: maybe lots of repeated values make construction easier. Nope. If there is no , then any segment containing a zero has MEX exactly .
Take any ordering. Let the first zero be at position and the last zero be at position , with . Split right after position . The prefix contains a zero, and the suffix also contains a zero. Since there is no anywhere, both sides have MEX . That split is bad, so no ordering can work:
The remaining case is:
Here we can explicitly build a valid order: put all zeroes first. The rest can be anything, but imagine sorted order if you like.
Check every split:
That covers all splits. So the final condition is simply:
We do not actually need to output the reordering, only whether it exists.
Complexity is per test case, with extra memory. Tiny constraints, tiny solution. Love when the problem folds like cheap cardboard.
#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;
int cnt0 = 0, cnt1 = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
cnt0 += (x == 0);
cnt1 += (x == 1);
}
bool ok = cnt0 > 0 && (cnt0 == 1 || cnt1 > 0);
cout << (ok ? "YES" : "NO") << '\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;
int cnt0 = 0, cnt1 = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
cnt0 += (x == 0);
cnt1 += (x == 1);
}
bool ok = cnt0 > 0 && (cnt0 == 1 || cnt1 > 0);
cout << (ok ? "YES" : "NO") << '\n';
}
return 0;
}