You are given an array a with n elements. Each element of a is either 0 or 1.
Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k.
The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones.
On the second line print n integers aj — the elements of the array a after the changes.
If there are multiple answers, you can print any one of them.
7 1 1 0 0 1 1 0 1
4 1 0 0 1 1 1 1
10 2 1 0 0 1 0 1 0 1 0 1
5 1 0 0 1 1 1 1 1 0 1
题意:把最多k个0变成1求最长的连续1的串.
两个指针每次保持之间的0个数恰好是k个.
#include <bits/stdc++.h>
using namespace std;
#define maxn 311111
int a[maxn];
int next[maxn], sum[maxn];
int n, m;
int main () {
// freopen ("in.txt", "r", stdin);
cin >> n >> m;
sum[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum[i] = sum[i-1]+(a[i]==0);
}
if (m == 0) {
int ans = 0, cur = 0;
for (int i = 1; i <= n; i++) {
if (a[i] == 1)
cur++;
else {
ans = max (ans, cur);
cur = 0;
}
}
ans = max (ans, cur);
cout << ans << "\n";
for (int i = 1; i <= n; i++) cout << a[i] << (i == n ? "\n" : " ");
return 0;
}
int l = 1, r = 1, ans = 0, x, y;
for (; r < n && sum[r+1] <= m; r++) {
}
ans = r-l+1, x = l, y = r;
for (l = 2; l <= n; l++) {
while (sum[r+1]-sum[l-1] <= m && r+1 <= n)
r++;
if (ans < r-l+1) {
ans = r-l+1;
x = l, y = r;
}
}
cout << ans << "\n";
for (int i = x; i <= y; i++) a[i] = 1;
for (int i = 1; i <= n; i++) cout << a[i] << (i == n ? "\n" : " ");
return 0;
}