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.
Terminal positions are exactly sorted strings of the form . If there is any pair 10, that pair alone is a legal subsequence with exactly one inversion.
Do not just check the total inversion parity. 0100 has an even number of inversions and Alice still wins. That shortcut drives straight into a wall.
Look at whether Alice can delete an odd-inversion subsequence while leaving a terminal string. A terminal remainder can always be described by a cut: keep some zeros on the left side and some ones on the right side.
For such a cut, all 1s on the left and all 0s on the right must be deleted. A left-side optional 0 flips the deleted inversion parity iff the number of 1s before it is odd. A right-side optional 1 flips it iff the number of 0s after it is odd.
So Bob wins exactly when both conditions hold: every 0 has an even number of 1s before it, and every 1 has an even number of 0s after it. Otherwise Alice can immediately delete to a sorted string.
The tempting wrong idea is to count the total inversions and check odd/even. Nope. The sample 0100 has inversions, but Alice still wins. So the total count is wearing a fake mustache here.
Call a string balanced if:
0 has an even number of 1s before it;1 has an even number of 0s after it.The whole answer is:
BobAliceNow let’s justify that instead of just vibe-coding it.
Terminal Positions
A player cannot move iff there is no subsequence with an odd number of inversions.
If the string contains any 1 before any later 0, then choosing just those two characters gives subsequence 10, which has exactly one inversion. So a move exists.
Therefore terminal strings are exactly sorted binary strings:
Why An Unbalanced String Is Winning
Suppose there is a 0 at position with an odd number of 1s before it.
We will leave a sorted string. Put a cut at . To make the remaining string sorted, delete all 1s on the left of the cut and all 0s on the right of the cut. The 0 at is optional: either keep it or delete it.
If we switch whether this 0 is deleted, the inversion parity of the deleted subsequence changes by the number of deleted 1s before it. But all 1s before it are deleted, and that number is odd. So the parity flips.
That means one of the two choices deletes a subsequence with odd inversion count. The remainder is sorted, so Alice wins instantly.
The argument is symmetric if there is a 1 with an odd number of 0s after it. Put the cut just before that 1; all those 0s after it are forced deleted, and choosing whether to delete this 1 flips the parity by an odd amount.
So if the string is not balanced, Alice has a one-move kill. Brutal, but fair.
Why A Balanced String Is Losing
Now assume the current string is balanced.
We prove Alice cannot move to another balanced string. That is enough, because every unbalanced string is immediately winning for the next player.
Let Alice delete subsequence and leave subsequence . Suppose, for contradiction, that is also balanced.
Count inversions inside modulo by looking at deleted 0s:
For each deleted 0, the number of total 1s before it in is even, because is balanced. So:
deleted ones before it has the same parity as kept ones before it.
Therefore, the inversion parity of equals the parity of the number of cross pairs:
kept 1 before deleted 0.
Now count those same cross pairs by kept 1s. For every kept 1, the total number of 0s after it in is even, since is balanced. Also, because is balanced, the number of kept 0s after it is even. Thus the number of deleted 0s after it is also even.
So every kept 1 contributes an even number of cross pairs. The total cross-pair count is even, hence the deleted subsequence has an even number of inversions.
But Alice’s move must delete a subsequence with an odd number of inversions. Contradiction.
So from a balanced string, every legal move leaves an unbalanced string. Then the next player immediately deletes to a terminal sorted string. Therefore balanced positions are losing.
Implementation
We only need parity.
First scan left to right:
1s seen;0 while this parity is odd, Alice wins.Then scan right to left:
0s seen;1 while this parity is odd, Alice wins.If neither scan finds a violation, print Bob.
Complexity: per test case, 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;
string s;
cin >> n >> s;
bool alice = false;
int onesParity = 0;
for (char c : s) {
if (c == '1') {
onesParity ^= 1;
} else {
if (onesParity) alice = true;
}
}
int zerosParity = 0;
for (int i = n - 1; i >= 0; --i) {
if (s[i] == '0') {
zerosParity ^= 1;
} else {
if (zerosParity) alice = true;
}
}
cout << (alice ? "Alice" : "Bob") << '\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;
string s;
cin >> n >> s;
bool alice = false;
int onesParity = 0;
for (char c : s) {
if (c == '1') {
onesParity ^= 1;
} else {
if (onesParity) alice = true;
}
}
int zerosParity = 0;
for (int i = n - 1; i >= 0; --i) {
if (s[i] == '0') {
zerosParity ^= 1;
} else {
if (zerosParity) alice = true;
}
}
cout << (alice ? "Alice" : "Bob") << '\n';
}
return 0;
}