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.
Think of each card as a vertex. The two rules add two kinds of edges that must connect opposite colors: neighbors by position, and neighbors by value.
The original row is just a path, so once you choose the color of the first position, every other position is forced by parity.
The sorted row is also just a path: values must alternate colors, so each value’s color is forced by the parity of the value, up to one global flip.
For the card at position with value , the position rule wants its color to depend on , while the sorted rule wants it to depend on .
So and must have the same relationship for every : either all cards sit on matching parity positions, or all sit on opposite parity positions.
The first rule says adjacent cards in the original row must have different colors. Since the original row is a path, this completely forces the coloring pattern once the first card is chosen:
for some fixed bit .
The second rule says that after sorting by value, consecutive values must also have different colors. Since the values are distinct and each is between and , the array is a permutation of . So the sorted row is also a path:
That rule forces:
for some fixed bit .
Now look at the actual card at position , whose value is . Both forced formulas must describe the same card, so:
Rearrange the constants to one side:
The right side is one fixed value for the whole array. Therefore, the condition is simple as hell: for every position , the parity relationship between and must be identical.
That means either:
If this holds, we can color by position parity, and the sorted order will automatically alternate too. If it does not hold, then two cards demand incompatible parity patterns, so no coloring can satisfy both paths.
Edge cases are boring in the best way. For , the same check works. The values are guaranteed distinct and in , so we do not need to handle duplicates or missing values.
Complexity per test case is:
with extra memory.
#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 need = -1;
bool ok = true;
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
int cur = (i & 1) ^ (a & 1);
if (need == -1) need = cur;
else if (cur != need) ok = false;
}
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 need = -1;
bool ok = true;
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
int cur = (i & 1) ^ (a & 1);
if (need == -1) need = cur;
else if (cur != need) ok = false;
}
cout << (ok ? "YES" : "NO") << '\n';
}
return 0;
}