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.
A class with 1 is never sleepable. That part is forced, no negotiation with your sleep schedule.
After every 1, the next positions are also forced awake, even if they are 0.
For each class, you only need to know the most recent important class before it.
A 0 at position is sleepable exactly when there is no 1 among positions .
Scan left to right. Keep last, the index of the latest 1. Count a 0 only if i > last + k.
The key thing: there is basically no choice here.
The statement says every important class must be attended. Once you attend an important class at position , you are forced to stay awake for classes
So the schedule is fully determined by where the 1s are. This is not some spicy DP problem hiding in the bushes. It's just: mark which positions are forced awake, then count the rest.
What Makes A Class Sleepable?
Class is sleepable if both conditions hold:
Why previous positions only? Because an important class affects the classes after it, not before it. A future 1 cannot ruin your current nap. Time does not run backwards, unfortunately or fortunately depending on the school day.
So for a zero at position , it is forced awake if there exists some important class such that
That means lies within the cooldown zone after a 1.
Left-To-Right Scan
Maintain last, the index of the most recent important class seen so far.
When we are at position :
s[i] == '1', this class is important, so update last = i.0. It is sleepable only if it is outside the forced-awake range from the latest 1:
If that inequality holds, count it.
Why is the latest 1 enough? Because among all previous important classes, the most recent one has the farthest-right forced range endpoint. If even the most recent 1 does not force you awake, then older ones definitely do not either.
Example Walkthrough
Take:
Using 0-based indices:
0, no previous 1, sleep.1, awake, set last = 1.1, awake, set last = 5.1, awake.Answer: .
Correctness Argument
For every position , the algorithm tracks last, the nearest important class before or at .
If , the class is important, so it cannot be slept through. The algorithm does not count it, which is correct.
If , then the only possible reason it cannot be slept through is that some previous important class forces wakefulness. The strongest such previous class is the most recent one, stored in last. If , then class is within the forced-awake range, so the algorithm correctly does not count it. If , then no previous important class can force class awake, because every older important class has an even smaller forced range endpoint. So the algorithm correctly counts it as sleepable.
Thus every class is counted exactly when it can be slept through, so the answer is correct.
Complexity
Each test case is scanned once.
time and extra memory.
With , this is extremely chill. The CPU is also sleeping through this one.
#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 last = -1000000;
int ans = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
last = i;
} else if (i > last + k) {
ans++;
}
}
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, k;
string s;
cin >> n >> k >> s;
int last = -1000000;
int ans = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1') {
last = i;
} else if (i > last + k) {
ans++;
}
}
cout << ans << '\n';
}
return 0;
}