题目链接:http://codeforces.com/problemset/problem/660/C
尺取法,每次遇到0的时候补一个1,直到补完或者越界为止。之后每次从左向右回收一个0点。记录路径用两个指针卡住,每次更新即可。
1 #include <algorithm> 2 #include <iostream> 3 #include <iomanip> 4 #include <cstring> 5 #include <climits> 6 #include <complex> 7 #include <fstream> 8 #include <cassert> 9 #include <cstdio> 10 #include <bitset> 11 #include <vector> 12 #include <deque> 13 #include <queue> 14 #include <stack> 15 #include <ctime> 16 #include <set> 17 #include <map> 18 #include <cmath> 19 20 using namespace std; 21 22 const int maxn = 666666; 23 int n, k; 24 int a[maxn]; 25 26 int main() { 27 // freopen("in", "r", stdin); 28 while(~scanf("%d %d", &n, &k)) { 29 for(int i = 1; i <= n; i++) { 30 scanf("%d", &a[i]); 31 } 32 int ll = 1; 33 int rr = 1; 34 int al = 0; 35 int ar = 0; 36 int cur = 0; 37 int ans = 0; 38 while(1) { 39 if(rr > n) break; 40 while(rr <= n && cur <= k) { 41 if(a[rr] == 0) { 42 if(cur == k) break; 43 cur++; 44 } 45 rr++; 46 } 47 if(rr - ll > ans) { 48 ans = rr - ll; 49 al = ll; 50 ar = rr - 1; 51 } 52 while(ll <= rr && a[ll] == 1) ll++; 53 cur--; 54 ll++; 55 } 56 printf("%d ", ans); 57 for(int i = 1; i <= n; i++) { 58 if(i >= al && i <= ar) printf("1 "); 59 else printf("%d ", a[i]); 60 } 61 printf(" "); 62 } 63 return 0; 64 }