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 about when a move produces 0. The operation returns 0 only if the whole chosen subarray is made of 1s. Any chosen subarray containing at least one 0 turns into 1.
If the current array is all 1s, Alice is instantly happy: the player to move can choose the whole array and make the final number 0. So all-ones arrays are winning for Alice.
Zeros are the dangerous stuff. When a player selects a segment containing a 0, that entire segment becomes one 1, so zeros can be deleted/absorbed. But if the array has at least two separated zeros, one move cannot necessarily make the whole array all 1s unless the chosen segment covers every zero.
The only structural detail that matters is the position of the first zero and last zero. If Alice can choose a segment covering all zeros without taking the whole array, she can leave Bob with an all-ones array; then Bob is forced to make final 0, so Alice wins.
Alice wins exactly when there is a 1 strictly outside the interval from the first zero to the last zero. Equivalently, the first element is 1, or the last element is 1, or there are no zeros at all. Otherwise, Bob wins. Yeah, it’s basically “are the zeros touching both ends?” — rude but effective.
We need decide who can force the final single value. Alice wants final 0, Bob wants final 1.
The move is:
Since the array is binary:
1, it becomes 0;0, it becomes 1.That’s the whole game. Everything else is just not getting baited by the notation.
Key Observation
The final value becomes 0 only if the final chosen segment is all 1s.
So if a player ever receives an array consisting only of 1s and length at least , the only sensible ending is to choose the whole array and make the final number 0. That is good for Alice, no matter whose turn it is.
This means Alice has a very direct plan: on her first move, try to turn the array into all 1s. Then Bob is stuck: if the array length is more than , every legal full-array ending gives 0; if the array length is already , it is 1, which would be bad for Alice, so she must avoid choosing the entire array unless it already gives 0.
When can Alice make the whole remaining array all 1s?
A move turns the chosen segment into 1 if the chosen segment contains at least one 0.
All elements outside the chosen segment stay unchanged. Therefore, after Alice's move, the whole array is all 1s exactly when:
1, and1, handing Bob the win like a gift-wrapped disaster.Let be the position of the first zero, and be the position of the last zero.
Alice can cover all zeros by choosing the segment . This segment becomes 1. The outside parts are all 1s by definition of first/last zero.
Now the important bit: this is a legal winning first move only if is not the whole array. That happens exactly when:
1 before the first zero, or1 after the last zero.So Alice wins if there is a 1 outside the first-zero-to-last-zero interval.
There is also the special case with no zeros. If the array is all 1s, Alice chooses the whole array immediately and gets final 0, so Alice wins.
What if zeros touch both ends?
Now suppose the array starts with 0 and ends with 0. Then and .
Alice cannot choose a proper segment containing all zeros, because all zeros span the whole array. Her options are:
1; Bob wins immediately;After any non-losing Alice move, Bob gets an array that still has at least one zero. Bob can now simply choose the entire current array. Since it contains a zero, it becomes final 1, and Bob wins.
So if the first and last elements are both 0, Bob wins. Brutal, but fair.
Final Rule
Alice wins iff:
a[0] == 1, ora[n-1] == 1.Otherwise Bob wins.
Since if there is at least one zero and both ends are zero, the first zero is at the start and the last zero is at the end, so Bob has the forced win.
The implementation is tiny:
Alice if all ones or either endpoint is 1;Bob.Complexity: per test case, memory besides the input array. The constraints are small enough that even a potato could run this, but we still write it cleanly.
#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);
bool hasZero = false;
for (int &x : a) {
cin >> x;
if (x == 0) hasZero = true;
}
if (!hasZero || a.front() == 1 || a.back() == 1) {
cout << "Alice\n";
} else {
cout << "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;
cin >> n;
vector<int> a(n);
bool hasZero = false;
for (int &x : a) {
cin >> x;
if (x == 0) hasZero = true;
}
if (!hasZero || a.front() == 1 || a.back() == 1) {
cout << "Alice\n";
} else {
cout << "Bob\n";
}
}
return 0;
}