CodeForces - 660C Hard Process (二分)好题

来源:互联网 发布:美人一笑也倾城网络剧 编辑:程序博客网 时间:2024/06/04 19:26
CodeForces - 660C
Hard Process
Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

Submit Status

Description

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).

Input

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.

Output

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.

Sample Input

Input
7 11 0 0 1 1 0 1
Output
41 0 0 1 1 1 1
Input
10 21 0 0 1 0 1 0 1 0 1
Output
51 0 0 1 1 1 1 1 0 1

Source

Educational Codeforces Round 11
//题意:输入n,k,接着下面一行输入n个数(0或1)
表示给你一个长度为n的序列,让你改变其中的k个0,使得这个序列中连续的1的长度达到最长。输出最长长度并输出改变后的序列。
//思路:
二分法枚举1的最长长度,逐个判断,直到找到最长的连续的1的序列,具体看代码。
#include<stdio.h>#include<string.h>#include<math.h>#include<set>#include<map>#include<stack>#include<queue>#include<algorithm>#include<iostream>#define INF 0x3f3f3f3f#define ull unsigned long long#define ll long long#define IN __int64#define N 300010#define M 1000000007using namespace std;int a[N],sum[N];int p,n,k;bool judge(int mid){int i,j;for(i=0;i+mid-1<n;i++){if(sum[i+mid-1]-sum[i-1]+k>=mid){p=i;return true;}}return false;}int main(){int i,j;while(scanf("%d%d",&n,&k)!=EOF){memset(sum,0,sizeof(sum));for(i=0;i<n;i++){scanf("%d",&a[i]);sum[i]=sum[i-1]+a[i];}int l=0,r=n+1,mid;p=-1;while(l<r-1)//枚举最长的1的个数 {mid=(l+r)/2;if(judge(mid))l=mid;elser=mid;}printf("%d\n",l);for(i=0;i<p;i++)printf("%d ",a[i]);for(i=p;i<p+l;i++)printf("1 ");for(i=max(p+l,0);i<n;i++)printf("%d ",a[i]);printf("\n");}return 0;}

0 0