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.
Once a value has been chosen, every value smaller than is dead forever. So positions in the original array are irrelevant; only frequencies of values matter.
Think about the state right after some value was chosen. There are copies of left, and all larger values are still untouched. Let this state be winning/losing for the next player.
From value , the only “interesting” jump is to a larger value with . If any such is a losing state, jumping there wins immediately.
If there is no reachable larger losing state, then the only useful moves are taking more copies of . That is just parity: after Arseniy removes one , the next player wins iff is odd, i.e. iff is even.
So Arseniy fails only in the boring case: every present value has odd frequency, and no two distinct present values differ by at most . Otherwise print YES. Checking adjacent present values is enough.
The game is monotone: after the last chosen value becomes , nobody can ever choose a value smaller than again.
So forget array positions. Let be the frequency of value .
Consider a state right after some value has just been chosen. Then:
Let be whether this state is winning for the player to move.
Arseniy wants to choose some value such that is winning for Egor.
More generally, define as the state where the last chosen value is , there are copies of remaining, and all larger values are still available.
From , the current player can:
which moves to .
Now define
If is true, then every state is winning: just jump to that losing .
If is false, then jumping never helps, and the game at value is pure parity:
and for ,
Therefore:
is losing exactly when:
That DP is already enough, but it collapses into a much simpler condition.
Arseniy can make Egor win if either of these happens:
If is even, then after Arseniy removes one , there are copies left, which is odd.
If no useful jump exists, Egor just takes equal values and wins by parity. If a useful jump exists, even better. So any even positive frequency immediately gives YES.
Suppose there are present values with
If is winning, Arseniy simply chooses .
If is losing, Arseniy chooses , and Egor jumps to , giving Dabir a losing state.
Either way, YES.
NO?Only when both nice things fail:
Then after Arseniy chooses any value , Egor only has an even number of equal values to play with and cannot jump anywhere. Egor loses. Brutally simple.
So the algorithm is:
YES.YES.NO.Adjacent values are enough because if any two present values are close, some adjacent pair in sorted order is also close.
For each test case we scan values from to :
time and
memory. Across all tests, this is
which easily fits.
#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, k;
cin >> n >> k;
vector<int> cnt(n + 1, 0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
cnt[x]++;
}
bool ok = false;
int prev = -1;
for (int x = 1; x <= n; x++) {
if (cnt[x] == 0) continue;
if (cnt[x] % 2 == 0) ok = true;
if (prev != -1 && x - prev <= k) ok = true;
prev = x;
}
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, k;
cin >> n >> k;
vector<int> cnt(n + 1, 0);
for (int i = 0; i < n; i++) {
int x;
cin >> x;
cnt[x]++;
}
bool ok = false;
int prev = -1;
for (int x = 1; x <= n; x++) {
if (cnt[x] == 0) continue;
if (cnt[x] % 2 == 0) ok = true;
if (prev != -1 && x - prev <= k) ok = true;
prev = x;
}
cout << (ok ? "YES" : "NO") << '\n';
}
return 0;
}