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. Spring 和 Mybatis 整合

    Spring 和 Mybatis 整合 Spring本身的Config文件: 在IDEA下面配置好文件后, 在WEB-INF下面有三个配置文件分别是web.xml, applicationContex ...

  2. [学习笔记] AD笔记

    Auto diff 深度学习基础知识,auto diff自动微分的笔记,tensorflow中的求导就是基于这个做的.多用于复杂神经网络求导.来自于一篇论文,没怎么看完,但是会算了,比较底层一点吧.. ...

  3. CPP-网络/通信:SSL功能和原理

    面对诸多的网上交易的风险,很多网上银行.网上商城在建立系统时都注意到了安全问题.而我们也经常可以看到很多网上银行.电子商务网站在强调本系统的安全性的时候,都说采用了基于数字证书的SSL加密传输.SSL ...

  4. 题解 P5051 【[COCI2017-2018#7] Timovi】

    看到这道题目,数据范围,心凉了一大截 这是没开O2的 而这是开了O2的 emm……本蒟蒻也无言以对呀 好了,回归正题,看到题目的标签,高性能,自然而然地想到了快读 相信做这题的大佬们一定知道吧! 快读 ...

  5. shell脚本,awk里面的BEGIN讲解。

    解释: BEGIN{}这个特殊的pattern最常用的就是 变量赋值. BEGIN这个pattern就是文件没开始读的时候 执行 awk 'BEGIN{FS=":";OFS=&qu ...

  6. libnet TCP示例

    [root@TD18 tmp]#gcc -o .c -lnet [root@TD18 tmp]#./ please enter Host address 11.11.11.11 please ente ...

  7. 解决cocos2dx 打包lua环境搭建问题( ImportError: No module named Cheetah.Template)

    将c++ 封装成lua调用时,显示一下错误: PYTHON_BIN not defined, use current python. generating userconf.ini... Genera ...

  8. 基于axios的vue插件,让http请求更简单

    ajax-plus 基于axios 的 Vue 插件 如何使用 npm 模块引入 首先通过 npm 安装 ```npm install --save ajax-plus or yarn add aja ...

  9. 大小端测试C实现

    int is_little_endian(void)//判断是否是小端的函数 { union check_fun { int a; char b; }u1; u1.a=;//先将1(实际上就是0x00 ...

  10. 牛客网暑期ACM多校训练营(第七场)A Minimum Cost Perfect Matching(找规律)

    题意: 给定n, 求一个0~n-1的全排列p, 使得的和最小 分析: 打表发现最优解肯定是和为0的, 然后如果为2的幂就是直接反转即可, 不然的话就要分开从前面到后面逐步拆分, 具体思想模拟一下n = ...