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.
Start by forgetting the exact reversal for a second and asking a simpler question: which pairs of elements can ever change their relative order? Two elements can cross only if some operation uses a segment containing both of them.
Take two values with the same parity. If every value of the opposite parity lies inside the value interval , then any segment containing both and has minimum and maximum of the same parity. That segment is never allowed. So that pair is frozen forever. Brutal.
Let be the minimum/maximum even value present, and the minimum/maximum odd value present. For parity , the dangerous elements are:
Now the punchline: those are the only truly frozen pairs. If two same-parity values are not of the form low/high, then there exists an opposite-parity value outside their interval, which can act like a catalyst; with reversals, you can eventually swap them around as needed. So the only invariant that matters is: for each parity, all low elements must stay before all high elements.
So the check is tiny:
NO.
If this never happens for either parity, answer is YES.
This is just per test. Nice and nasty.The whole problem looks like one of those “ah yes, let’s just reverse stuff and pray” tasks. Don’t. The trick is to understand what can never happen.
Two elements can change relative order only if some chosen reversed segment contains both of them. If every operation that contains them is forbidden, then their order is permanent.
So fix two values of the same parity.
Suppose that all values of the opposite parity lie inside the value interval . Then any segment containing both and has:
and since have the same parity, the minimum and maximum also have the same parity. Thus so such a segment is never allowed.
That means this pair can never cross.
Let
Assume both parities exist.
For parity , define:
Why are these special?
Take a low- value and a high- value . All values of the opposite parity lie between them in value, i.e. So and are exactly the frozen kind of pair above. They can never change their relative order.
That’s the “reserved” part. Those pairs are locked.
Nope — that’s the entire game.
If two same-parity values are not a low/high pair, then there exists an opposite-parity value outside their interval. That opposite-parity value can serve as a catalyst, and with allowed reversals you can rearrange such pairs when needed. Opposite-parity adjacent swaps are trivially allowed anyway, because for length we just need the two values to have different parity.
So the only genuine invariant is:
For each parity, every low element must remain before every high element.
If the initial array violates that for some parity, then sorted order is impossible. If it doesn’t violate that, then all remaining inversions are fixable.
Then every segment has minimum and maximum of the same parity, so no operation is allowed at all.
So in that case the answer is simply whether the array is already non-decreasing.
No parity mix, no party. Very rude problem.
If the array contains both parities, then for each parity :
Scan the array from left to right.
If you ever see a high- element, and later see a low- element, then answer is NO.
Do this for both parities.
If neither scan fails, answer is YES.
For a fixed parity , all low- elements must come before all high- elements in every reachable array. So if the original array already has some high- before some low-, you’re dead.
Conversely, if that bad pattern never appears, then the frozen order constraints are already consistent with the sorted array, and everything else is movable enough to finish the sort.
So the whole problem collapses to two simple one-pass checks.
For each test case:
Total complexity is per test case, and over all test cases.
Memory usage is if you store the array, or extra besides input storage.
Short, mean, and effective.
#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> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
bool hasEven = false, hasOdd = false;
int mnEven = (int)1e9, mxEven = -(int)1e9;
int mnOdd = (int)1e9, mxOdd = -(int)1e9;
for (int x : a) {
if (x % 2 == 0) {
hasEven = true;
mnEven = min(mnEven, x);
mxEven = max(mxEven, x);
} else {
hasOdd = true;
mnOdd = min(mnOdd, x);
mxOdd = max(mxOdd, x);
}
}
if (!hasEven || !hasOdd) {
cout << (is_sorted(a.begin(), a.end()) ? "YES\n" : "NO\n");
continue;
}
auto ok_for_parity = [&](int p, int oppMin, int oppMax) -> bool {
bool seenHigh = false;
for (int x : a) {
if ((x & 1) != p) continue;
if (x > oppMax) seenHigh = true; // high-p
if (x < oppMin && seenHigh) return false; // low-p after high-p
}
return true;
};
bool ok = true;
ok &= ok_for_parity(0, mnOdd, mxOdd); // even values vs odd span
ok &= ok_for_parity(1, mnEven, mxEven); // odd values vs even span
cout << (ok ? "YES\n" : "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;
vector<int> a(n);
for (int i = 0; i < n; ++i) cin >> a[i];
bool hasEven = false, hasOdd = false;
int mnEven = (int)1e9, mxEven = -(int)1e9;
int mnOdd = (int)1e9, mxOdd = -(int)1e9;
for (int x : a) {
if (x % 2 == 0) {
hasEven = true;
mnEven = min(mnEven, x);
mxEven = max(mxEven, x);
} else {
hasOdd = true;
mnOdd = min(mnOdd, x);
mxOdd = max(mxOdd, x);
}
}
if (!hasEven || !hasOdd) {
cout << (is_sorted(a.begin(), a.end()) ? "YES\n" : "NO\n");
continue;
}
auto ok_for_parity = [&](int p, int oppMin, int oppMax) -> bool {
bool seenHigh = false;
for (int x : a) {
if ((x & 1) != p) continue;
if (x > oppMax) seenHigh = true; // high-p
if (x < oppMin && seenHigh) return false; // low-p after high-p
}
return true;
};
bool ok = true;
ok &= ok_for_parity(0, mnOdd, mxOdd); // even values vs odd span
ok &= ok_for_parity(1, mnEven, mxEven); // odd values vs even span
cout << (ok ? "YES\n" : "NO\n");
}
return 0;
}