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.
Try using shift repeatedly. What happens to every 1 after one operation? After two? It starts spreading to the right like a very cheap infection.
After doing shift exactly times, every position within distance to the right of some original 1 becomes 1.
So if the longest cyclic block of consecutive 0s has length , then operations of shift are always enough.
Now prove you cannot do better. Look at the last 0 inside a longest zero block. Its nearest original 1 to the left is exactly steps away.
Any sequence of operations with total cost less than can only move information less than total steps to the right, so that last zero cannot be reached. Therefore the answer is just the longest cyclic run of 0s.
The operation looks more complicated than it is. Strip away the wording and it says this:
If a position is currently 1, then choosing shift can make the position steps to its right become 1 too. This happens for all current 1s at once.
So 1s spread clockwise around the cyclic string.
The obvious strategy is already optimal
Suppose we choose .
Then every current 1 copies itself one position to the right. If we do that again, the newly created 1s also copy themselves one more position to the right.
After doing shift exactly times, every position whose nearest original 1 on the left is at distance at most becomes 1.
That means the only thing stopping us is the longest cyclic block of consecutive 0s.
Let that length be .
For example, in:
10101010100there is a cyclic zero block of length at the end: 00. So the answer is .
Do shift twice, and every zero gets swallowed. Very cinematic. Very low budget.
Why the answer is at most
Every block of zeros sits immediately after some 1 on the circle.
If a zero block has length , then doing shift repeatedly fills it from left to right:
11Since every zero block has length at most , after shifts of size , all zero blocks are gone.
So:
Why the answer is at least
Now take one longest cyclic block of zeros, length .
It looks like this around the circle:
1 0 0 0 ... 0 1
<--- g zeros --->Focus on the last zero in this block, the one right before the next original 1.
From the original 1 on the left, this zero is exactly steps to the right. There is no original 1 closer to it from the left, because this whole stretch is zeros.
Now imagine trying to make this position 1 with total cost less than .
Each operation shifts copied 1s to the right by its chosen , and costs exactly . If the total cost is less than , then along any chain of copied 1s, you have moved less than total steps to the right.
So you cannot reach that last zero from the previous original 1. And no other original 1 reaches it faster clockwise, because this was a longest zero gap and this target is inside it.
Therefore any successful plan must spend at least coins:
Combine both sides:
So the entire problem is just: find the longest cyclic run of 0s. The scary operation text was mostly there to mug you in an alley.
How to compute the longest cyclic zero run
Because the string is cyclic, a zero run can wrap around the end into the beginning.
The clean trick is to scan s + s and count consecutive zeros, while capping the answer at .
Since the statement guarantees at least one 1, the true answer is never , but the cap keeps the scan honest.
For each character in s+s:
0, increase the current zero-run lengthmin(current, n)That best value is the answer.
Complexity
For each test case, we scan characters, so the time complexity is:
The sum of all is at most , so this is easily fast enough.
#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;
int ans = 0, cur = 0;
for (int i = 0; i < 2 * n; i++) {
if (s[i % n] == '0') cur++;
else cur = 0;
ans = max(ans, min(cur, n));
}
cout << ans << '\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;
int ans = 0, cur = 0;
for (int i = 0; i < 2 * n; i++) {
if (s[i % n] == '0') cur++;
else cur = 0;
ans = max(ans, min(cur, n));
}
cout << ans << '\n';
}
return 0;
}