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 hike uses a block of exactly consecutive zeroes. A day with rain is never inside a hike, so it naturally splits the timeline into independent chunks of good weather.
Look at one maximal streak of zeroes of length . All hikes inside this streak need hiking days each, and between two hikes inside the same streak you must waste at least break day.
If you make hikes in one zero-streak, they consume hiking days plus required break days between them. So you need .
Rearrange that inequality: , so the best number of hikes in a zero-streak is .
Scan the array, count lengths of consecutive zero-runs, add for each run, and reset the count whenever you hit a .
The problem is basically about packing hikes into stretches of good weather.
A hike needs exactly consecutive good-weather days. After a hike, Jean cannot start another hike on the very next day, so between two hikes there must be at least one unused day.
The key thing: rainy days split the problem. A hike cannot cross a rainy day, so every maximal block of zeroes can be handled independently.
For example, if the array has a good-weather streak like:
and , then Jean can do:
That gives hikes.
One zero-streak
Suppose we have a maximal streak of good weather of length .
If Jean makes hikes inside this streak:
So the total number of days needed is:
This must fit inside the streak:
Simplify:
So the maximum possible is:
That is the whole trick. Tiny formula, very Codeforces 800, no casino required.
Why this is optimal
Inside a zero-streak, starting hikes as early as possible is never bad. If you delay a hike, you only waste good-weather days before it, and that cannot create more room later. The densest valid pattern is:
So each completed hike costs days, except the final hike does not need a break after it. That is exactly why the formula is instead of just .
A common wrong idea is to count every subarray of length containing only zeroes. That overcounts badly because overlapping hikes are not allowed, and even back-to-back hikes are illegal without a break day. For and seven zeroes, there are five all-zero windows, but the answer is only two. Brutal, but fair.
Algorithm
Scan the array from left to right:
cur = current length of consecutive zeroes.cur.cur=0.The total complexity is per test case, and since the sum of all is at most , this easily fits.
#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;
cin >> n >> k;
int ans = 0;
int cur = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x == 0) {
cur++;
} else {
ans += (cur + 1) / (k + 1);
cur = 0;
}
}
ans += (cur + 1) / (k + 1);
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;
cin >> n >> k;
int ans = 0;
int cur = 0;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
if (x == 0) {
cur++;
} else {
ans += (cur + 1) / (k + 1);
cur = 0;
}
}
ans += (cur + 1) / (k + 1);
cout << ans << '\n';
}
return 0;
}