题目链接:http://codeforces.com/problemset/problem/660/C

尺取法,每次遇到0的时候补一个1,直到补完或者越界为止。之后每次从左向右回收一个0点。记录路径用两个指针卡住,每次更新即可。

 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; const int maxn = ;
int n, k;
int a[maxn]; int main() {
// freopen("in", "r", stdin);
while(~scanf("%d %d", &n, &k)) {
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
}
int ll = ;
int rr = ;
int al = ;
int ar = ;
int cur = ;
int ans = ;
while() {
if(rr > n) break;
while(rr <= n && cur <= k) {
if(a[rr] == ) {
if(cur == k) break;
cur++;
}
rr++;
}
if(rr - ll > ans) {
ans = rr - ll;
al = ll;
ar = rr - ;
}
while(ll <= rr && a[ll] == ) ll++;
cur--;
ll++;
}
printf("%d\n", ans);
for(int i = ; i <= n; i++) {
if(i >= al && i <= ar) printf("1 ");
else printf("%d ", a[i]);
}
printf("\n");
}
return ;
}
05-11 21:57