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.
Only positions containing 1 matter. A 0 never changes, so protecting it is just lighting a move on fire.
A 1 is safe without protection if there is already some earlier 1 among the previous positions.
Because Teto scans left to right, once you make sure an earlier 1 cannot disappear, it can protect later nearby 1s for free.
Look at the positions of all 1s. If two consecutive 1s are less than apart, protecting the earlier “chain start” is enough to block the later one.
So each group of 1s where consecutive positions differ by less than needs exactly one protected position: the first 1 in that group. Count how many times a 1 has no previous 1 within distance .
Let the positions of the 1s be
If there are no 1s, the answer is obviously , because Teto has nothing to change.
Now focus on some 1 at position . Teto can change it to 0 exactly when there is no 1 in the previous positions. In other words, it is vulnerable if the nearest earlier remaining 1 is at distance at least , or does not exist.
The key detail: the scan goes left to right. So a protected 1 can act like a blocker for later 1s. If a later 1 is within distance , then Teto sees that earlier 1 and cannot change the later one.
So the string breaks into chains of 1s:
1s are less than apart, they belong to the same chain.1 starts a new chain, because nothing before it can block it.For each chain, we must protect at least one position. Specifically, the first 1 of the chain must be protected, because there is no earlier 1 close enough to save it. If we do not protect it, Teto can immediately delete it. Brutal, but fair.
Protecting the first 1 of a chain is also sufficient. Then the next 1 in the chain has a previous 1 within the last positions, so it cannot be changed. The next one is blocked by some earlier 1, and so on. By induction, every 1 in the chain stays unchanged.
Therefore the answer is simply:
Count every
1whose previous1is either missing or at distance at least .
That is the first 1 of each chain.
Algorithm:
last, the index of the previous 1.1:
1, or i - last >= k, this starts a new chain, so increment the answer;last = i.The update always happens because every 1 can help block later 1s once the chain’s first 1 is protected.
Complexity is per test case, which is tiny here.
#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;
string s;
cin >> n >> k >> s;
int ans = 0;
int last = -1e9;
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
if (i - last >= k) ans++;
last = i;
}
}
cout << ans << '\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, k;
string s;
cin >> n >> k >> s;
int ans = 0;
int last = -1e9;
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
if (i - last >= k) ans++;
last = i;
}
}
cout << ans << '\n';
}
}