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.
Souvlaki can reorder first, so the original order is bait. Sort the multiset and think about the final target array.
After round , position is locked forever. Later rounds only touch positions to the right.
Kalamaki controls even rounds: . On those rounds, Souvlaki must survive both choices: swap or skip.
If Kalamaki controls two different values at positions and , he can choose which one gets locked into position . That is only harmless if both values are equal.
So in the sorted array , every even-indexed pair must be equal: In 0-based indexing, check a[1] == a[2], a[3] == a[4], etc.
Let the sorted version of the multiset be
If Souvlaki wins, the final array must be exactly this sorted sequence of values.
The game processes adjacent edges from left to right. Round is the only time positions and can be swapped. After round ends, position will never be touched again. So the final array gets fixed one position at a time from left to right.
Now look at Kalamaki’s turns. He plays on even rounds:
At an even round , Kalamaki decides whether to swap positions and . Since position becomes locked immediately after this, Souvlaki must be okay with both choices.
That means the two possible values Kalamaki can lock into position must both be valid. But the sorted target needs position to contain . If those two values are different, Kalamaki can choose the wrong one. So the only safe case is when the two values are equal.
Therefore, in the sorted target, every Kalamaki-controlled boundary must have equal values:
This condition is also enough. If it holds, Souvlaki simply arranges the array in sorted order before the game starts. On every odd round, he skips. On every even round, Kalamaki is swapping two equal adjacent values, so the array does not change. Kalamaki gets to make moves, but they do absolutely nothing. Brutal, but fair.
If the condition fails for some even , then . When Kalamaki controls that boundary, Souvlaki would need position to become exactly no matter what Kalamaki chooses. That would require both candidate values to be , meaning there would still be another after position . But if , there is no extra copy of after position in sorted order. Contradiction. So Souvlaki cannot guarantee a win.
Algorithm:
YES; otherwise print NO.In 0-based indexing, check indices (1,2), (3,4), (5,6), etc.
Complexity: per test case, easily fine for .
#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 &x : a) cin >> x;
sort(a.begin(), a.end());
bool ok = true;
for (int i = 1; i + 1 < n; i += 2) {
if (a[i] != a[i + 1]) {
ok = false;
break;
}
}
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;
vector<int> a(n);
for (int &x : a) cin >> x;
sort(a.begin(), a.end());
bool ok = true;
for (int i = 1; i + 1 < n; i += 2) {
if (a[i] != a[i + 1]) {
ok = false;
break;
}
}
cout << (ok ? "YES" : "NO") << '\n';
}
return 0;
}