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.
The endpoints are boring but important: positions and can never be changed. Treat them as anchors, because every move only touches an internal character.
A position can be changed exactly when it has two neighboring 's. So a pattern is powerful: the middle can become , and a pattern is also powerful: the middle can become .
Forget the exact original zeros for a second. What matters is the segment from the first to the last : outside it, zeros have no on both sides, so they are stuck forever.
Inside the segment , you can make everything , so the maximum is easy. For the minimum, you want surviving positions as sparse as possible while still having both endpoints and remain .
If , the maximum is . The minimum is the fewest points needed to go from to using jumps of size at most , which is . That's the whole damn trick.
Let be the first index with , and let be the last one.
If there is no , no move can ever happen, so the answer is simply:
Now assume at least one exists.
Positions outside can never become . A position can only be changed if it has 's on both sides:
Outside the first/last , one side has no available at all. So all action is trapped inside .
Inside , we can fill everything with 's. Whenever two 's are separated, the operation can gradually create more 's between them. So eventually the whole segment becomes all 's.
Thus:
For the minimum, we want as few surviving 's as possible, but and cannot be changed if they are endpoints of the whole reachable segment.
Between surviving 's, the gap cannot exceed . If two remaining positions were distance or more apart, the middle area could not be supported by moves correctly. The sparsest valid pattern is basically:
So if , we need the fewest jumps of length at most to cover distance :
jumps, meaning:
In integer C++ arithmetic:
We scan the string once.
per test case, with extra memory. Constraints are tiny, but the solution is still clean.
#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 l = -1, r = -1;
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
if (l == -1) l = i;
r = i;
}
}
if (l == -1) {
cout << "0 0\n";
continue;
}
int d = r - l;
int mn = (d + 1) / 2 + 1;
int mx = d + 1;
cout << mn << ' ' << mx << '\n';
}
}#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 l = -1, r = -1;
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
if (l == -1) l = i;
r = i;
}
}
if (l == -1) {
cout << "0 0\n";
continue;
}
int d = r - l;
int mn = (d + 1) / 2 + 1;
int mx = d + 1;
cout << mn << ' ' << mx << '\n';
}
}