题目链接:http://codeforces.com/problemset/problem/660/C
题目大意:给你一个由1 和 0组成的数组, 最多改变k次,0->1, 1—>0, 问改变后连续是 1 的长度最大是多少, 并输出改变后的数组
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cstdlib> 6 #include <cmath> 7 #include <set> 8 #include <map> 9 #include <vector> 10 #define N 300010 11 using namespace std; 12 13 int a[N], b[N];//b[i] 表示i 以前的0的个数 14 int main() 15 { 16 int n, k; 17 scanf("%d %d", &n, &k); 18 b[0] = 0; 19 for(int i = 1; i <= n; i++) 20 { 21 scanf("%d", a + i); 22 b[i] = b[i - 1] + 1 - a[i]; 23 } 24 int ii = 0, jj = 0, max = 0;//max记录连续1最长的长度,ii和jj记录起点和终点 25 if(b[n] <= k) 26 ii = 1, jj = n, max = n; 27 for(int i = 1; i <= n; i++) 28 { 29 if(b[n] >= b[i - 1] + k + 1){ 30 int pos = upper_bound(b + 1, b + n + 1, b[i - 1] + k) - b; 31 if(pos - i > max){ 32 max = pos - i; 33 ii = i ; 34 jj = pos - 1; 35 } 36 } 37 if(b[n] == b[i - 1] + k) 38 { 39 if(n - i + 1 > max) 40 { 41 max = n - i + 1; 42 ii = i, jj = n; 43 } 44 break; 45 } 46 } 47 printf("%d ", max); 48 for(int i = 1; i <= n; i++) 49 { 50 if(i >= ii && i <= jj){ 51 if(i != n ) 52 printf("1 "); 53 else 54 printf("1 "); 55 } 56 else{ 57 if(i != n ) 58 printf("%d ", a[i]); 59 else 60 printf("%d ", a[i]); 61 } 62 } 63 return 0; 64 }