Codeforces 660C Hard Process (er)

C. Hard Process
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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


123

[题意]

 把不超过k个的0 变成1 使1 的长度最大.

[思路]

 用二分的方法, 二分区间,确定长度.特别好的思路, 还有用迟取法也是可以的. 这里用二分, 二分是个很玄学的东西.

把区间二分后, 从0-n-mid判断 加上k个是否满足. 记录 开始值 i 为了 记录数组用

[代码实现]

#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x,b,n) lower_bound(b+1,b+1+n,x)-b
#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt","w",stdout)
#define SHUT ios_base::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(20); cout.tie(nullptr); cin.tie(nullptr);
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define  FI(n) IO::read(n)
#define  Be IO::begin()

using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int MAXN=300005;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};

int a[MAXN];
int sum[MAXN];
int n,k;
int temp;
int judge(int mid)
{
    for(int i=0;i<=n-mid;i++)
    {
        if(sum[i+mid]-sum[i]+k>=mid)
        {
          temp=i;
          return 1;
        }
    }
    return 0;
}
int main()
{

    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        sum[i]=sum[i-1]+(a[i]==1);
    }
    temp=-1;
    int l=0,r=n+1;
    while(l+1<r)
    {
        int mid=(l+r)>>1;
        if(judge(mid))
            l=mid;
        else
            r=mid;
    }
    cout<<l<<endl;
    //cout<<temp<<endl;
    for(int i=1;i<=temp;i++)
        cout<<a[i]<<" ";
    for(int i=temp+1;i<=temp+l;i++)
        cout<<"1 ";
    for(int i=max(temp+l+1,1);i<=n;i++)
        cout<<a[i]<<" ";
    cout<<endl;
    return 0;
}


posted @ 2018-01-28 20:20  Sizaif  阅读(191)  评论(0编辑  收藏  举报