C. Hard Process
题意:
给一个01串,最多可以把k个0变成1,求最长连续1的长度,并且要输出最后的串。
题解:
two pointers尺取,
O(N)
。
#include<bits/stdc++.h>
using namespace std;
const int N = 3e5+5;
int num[N];
int main(){
int n, k;
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; ++i) scanf("%d", num+i);
int l = 1, r = 1, zr = 0, ans = 0, ansl = 1, ansr = 0;
while(r <= n){
while(r <= n && zr <= k){
if(!num[r]){
if(zr == k) break;
else ++zr;
}
++r;
}
if(r-l > ans) ans = r-l, ansl = l, ansr = r-1;
while(l <= n && num[l]) ++l;
--zr, ++l;
}
printf("%d\n", ans);
for(int i = 1; i <= n; ++i){
if(ansl <= i && i <= ansr) printf("1 ");
else printf("%d ", num[i]);
}
}