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

记录0出现的位置,随便写几组数据可以发现要判断起始点是否为0的情况。

若起始点为0,则要判断sum[r]-sum[l]+1<=k;否则要判断sum[r]-sum[l]<=k。然后再判断一下k是否为0即可

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int N=300010;
int sufix[N];
int pos[N];
int main (void)
{
ios::sync_with_stdio(false);
int n,k,i,j;
while (cin>>n>>k)
{
memset(sufix,0,sizeof(sufix));
memset(pos,0,sizeof(pos));
for (i=1; i<=n; i++)
{
cin>>pos[i];
sufix[i]=sufix[i-1]+(pos[i]==0);
}
int l,r,dx,al,ar;
l=1,r=1,dx=0,ar=al=0;
while (l<=n&&r<=n)
{
bool flag=0;
if(pos[l])
{
if(sufix[r]-sufix[l]<=k)
{
if(r-l+1>=dx)
{
dx=r-l+1;
al=l;
ar=r;
}
}
else
{
l++;
}
r++;
}
else
{
if(sufix[r]-sufix[l]+1<=k)
{
if(r-l+1>=dx)
{
dx=r-l+1;
al=l;
ar=r;
}
}
else
{
l++;
}
r++;
}
}
for (i=al; i<=ar; i++)
{
pos[i]=1;
}
if(ar==0&&al==0)
cout<<0<<endl;
else
cout<<ar-al+1<<endl;
for (i=1; i<=n; i++)
{
if(i==n)
cout<<pos[i]<<endl;
else
cout<<pos[i]<<" ";
}
}
return 0;
}

  

Educational Codeforces Round 11——C. Hard Process(YY)的更多相关文章

  1. Educational Codeforces Round 11 C. Hard Process 二分

    C. Hard Process 题目连接: http://www.codeforces.com/contest/660/problem/C Description You are given an a ...

  2. Educational Codeforces Round 11 C. Hard Process 前缀和+二分

    题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...

  3. Educational Codeforces Round 11——A. Co-prime Array(map+vector)

    A. Co-prime Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. Educational Codeforces Round 15 C. Cellular Network(二分)

    C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  5. Educational Codeforces Round 43 E. Well played!(贪心)

    E. Well played! time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. Educational Codeforces Round 37 E. Connected Components?(图论)

    E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  7. [Educational Codeforces Round 63 ] D. Beautiful Array (思维+DP)

    Educational Codeforces Round 63 (Rated for Div. 2) D. Beautiful Array time limit per test 2 seconds ...

  8. Codeforces Round 662 赛后解题报告(A-E2)

    Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...

  9. Codeforces Round #306 (Div. 2) ABCDE(构造)

    A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全, ...

随机推荐

  1. linux yum安装指定版本mysql

    1.下载mysql rpm包 cd /usr/local/src wget https://dev.mysql.com/get/mysql80-community-release-el7-.noarc ...

  2. [VC]WindowProc和DefWindowProc函数

    在Windows操作系统里,当窗口显示之后,它就可以接收到系统源源不断地发过来的消息,然后窗口就需要处理这些消息,因此就需要一个函数来处理这些消 息.在API里定义了一个函数为回调函数,当系统需要向窗 ...

  3. noip模拟赛#14

    #14: T1:f[x]=x-1(x&1)||x/2(x&1=0) 求[n,m]有多少个数可以通过变换得到k.(1e9). =>好像cf上看过类似的题,用二进制的方式来写.不过我 ...

  4. 在ProgressBar控件中显示进度百分比

    实现效果: 知识运用: ProgressBar控件的Value属性 //控件的当前值 Maximum属性 //ProgressBar正在使用的范围的上限 PerformStep方法 //按照Step属 ...

  5. WINDOWS-API:关于线程CreateThread,_beginthead(_beginthreadex),AfxBeginThread

    [转]windows多线程编程CreateThread,_beginthead(_beginthreadex)和AfxBeginThread的区别 在Windows的多线程编程中,创建线程的函数主要有 ...

  6. 01_1_准备ibatis环境

    01_1_准备ibatis环境 1. 搭建环境:导入相关的jar包 mysql-connector-java-5.1.5-bin.jar(mysql)或者ojdbc6.jar(oracle).ibat ...

  7. Java基础面试操作题:Java代理工厂设计模式 ProxyFactory 有一个Baby类,有Cry行为,Baby可以配一个保姆 但是作为保姆必须遵守保姆协议:能够处理Baby类Cry的行为,如喂奶、哄睡觉。

    package com.swift; public class Baby_Baomu_ProxyFactory_Test { public static void main(String[] args ...

  8. C# 调用腾讯地图WebService API获取距离(一对多)

    官方文档地址:https://lbs.qq.com/webservice_v1/guide-distance.html 代码: /// <summary> /// 获取距离最近的点的经纬度 ...

  9. Codevs1081 线段树练习 2

    题目描述 Description 给你N个数,有两种操作 1:给区间[a,b]的所有数都增加X 2:询问第i个数是什么? 输入描述 Input Description 第一行一个正整数n,接下来n行n ...

  10. logback写日志

    https://blog.csdn.net/u010128608/article/details/76618263 https://blog.csdn.net/zhuyucheng123/articl ...