#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, d; cin >> n >> d;
vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i];
vector<int> f(n, INT_MAX), r(n, INT_MAX);
f[0] = r[n-1] = 0;
deque<int> window; window.push_back(0);
for (int i = 0; i < n; i++) {
while (abs(window.front() - i) > d) window.pop_front();
f[i] = a[i] ? f[window.front()] : f[window.front()] + 1;
while (!window.empty() && f[window.back()] > f[i]) window.pop_back();
window.push_back(i);
}
window.assign(1, n-1);
for (int i = n-1; i >= 0; i--) {
while (abs(window.front() - i) > d) window.pop_front();
r[i] = a[i] ? r[window.front()] : r[window.front()] + 1;
while (!window.empty() && r[window.back()] > r[i]) window.pop_back();
window.push_back(i);
}
int x = INT_MAX; for (int i = 0; i < n; i++) x = min(x, a[i] ? f[i] + r[i] : f[i] + r[i] - 1);
cout << x << "\n";
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |