codeforces660C

Hard Process

 CodeForces - 660C 

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.

Examples

Input
7 1
1 0 0 1 1 0 1
Output
4
1 0 0 1 1 1 1
Input
10 2
1 0 0 1 0 1 0 1 0 1
Output
5
1 0 0 1 1 1 1 1 0 1

sol:只能把0变成1,而且要让连续的一段尽可能的大,于是可以枚举右端点二分左端点,判断那一段0的个数与K的关系
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
    ll s=0;
    bool f=0;
    char ch=' ';
    while(!isdigit(ch))
    {
        f|=(ch=='-'); ch=getchar();
    }
    while(isdigit(ch))
    {
        s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
    }
    return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
    if(x<0)
    {
        putchar('-'); x=-x;
    }
    if(x<10)
    {
        putchar(x+'0');    return;
    }
    write(x/10);
    putchar((x%10)+'0');
    return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('
')
const int N=300005;
int n,m,a[N],S[N];
int Ans[N];
inline int TwoFind(int l,int r)
{
    int Pos=r,ans=r+1;;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(S[Pos]-S[mid-1]<=m)
        {
            ans=mid; r=mid-1;
        }
        else l=mid+1;
    }
    return ans;
}
int main()
{
    int i,Pos=0;
    R(n); R(m);
    for(i=1;i<=n;i++)
    {
        R(a[i]); S[i]+=S[i-1]+(a[i]==0);
        int oo=TwoFind(1,i);
        Ans[i]=i-oo+1;
        if(Ans[i]>Ans[Pos]) Pos=i;
    }
    Wl(Ans[Pos]);
    for(i=Pos;i>=1&&m;i--) if(a[i]==0) a[i]=1,m--;
    for(i=1;i<=n;i++) W(a[i]);
    return 0;
}
/*
Input
7 1
1 0 0 1 1 0 1
Output
4
1 0 0 1 1 1 1

Input
10 2
1 0 0 1 0 1 0 1 0 1
Output
5
1 0 0 1 1 1 1 1 0 1
*/
View Code
 
原文地址:https://www.cnblogs.com/gaojunonly1/p/10748161.html