题目链接:

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 题意: 最多把k个0变成1,变完后连续的最长的全是1的串的长度是多少,并且输出最后得串; 思路: 用一个数组记录当前位一共有多少个0,暴力枚举最长串的最后一位,二分查找最长串的第一个1的位置;更新结果并记录好最长串的开始和结束位置,最后再输出就好啦; AC代码:
/*
2014300227 660C - 5 GNU C++11 Accepted 93 ms 4388 KB
*/
#include <bits/stdc++.h>
using namespace std;
const int N=3e5+;
typedef long long ll;
const double PI=acos(-1.0);
int n,a[N],k,b[N];
int check(int x,int y)
{
if(b[y]-b[x-]<=k)return ;
return ;
}
int bis(int num)
{
int l=,r=num,mid;
while(l<=r)
{
mid=(l+r)>>;
if(check(mid,num))r=mid-;
else l=mid+;
}
return r+;
}
int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
if(!a[i])b[i]=b[i-]+;
else b[i]=b[i-];
}
int ans=,l=,r=;
for(int i=;i<=n;i++)
{
int fx=bis(i);
if(i-fx+>ans)
{
ans=i-fx+;
l=fx;
r=i;
}
}
printf("%d\n",ans);
for(int i=;i<=n;i++)
{
if(i>=l&&i<=r)
{
printf("1 ");
}
else printf("%d ",a[i]);
}
return ;
}

codeforces 660C C. Hard Process(二分)的更多相关文章

  1. Hard Process(二分)

    Hard Process Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submi ...

  2. hdu 3433 A Task Process 二分+dp

    A Task Process Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  3. Codeforces 660C - Hard Process - [二分+DP]

    题目链接:http://codeforces.com/problemset/problem/660/C 题意: 给你一个长度为 $n$ 的 $01$ 串 $a$,记 $f(a)$ 表示其中最长的一段连 ...

  4. Codeforces 660C Hard Process【二分 Or 尺取】

    题目链接: http://codeforces.com/problemset/problem/660/C 题意: 给定0.1组成的数组,可以改变k个0使其为1,问最终可以得到的连续的1的最大长度. 分 ...

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

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

  6. Codeforces 660C Hard Process(尺取法)

    题目大概说给一个由01组成的序列,要求最多把k个0改成1使得连续的1的个数最多,输出一种方案. 和CF 676C相似. #include<cstdio> #include<algor ...

  7. codeforces 660C Hard Process

    维护一个左右区间指针就可以. #include<cstdio> #include<cstring> #include<iostream> #include<q ...

  8. CodeForces 377B---Preparing for the Contest(二分+贪心)

    C - Preparing for the Contest Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d ...

  9. Codeforces 484B Maximum Value(高效+二分)

    题目链接:Codeforces 484B Maximum Value 题目大意:给定一个序列,找到连个数ai和aj,ai%aj尽量大,而且ai≥aj 解题思路:类似于素数筛选法的方式,每次枚举aj,然 ...

随机推荐

  1. 在 Linux 多节点安装配置 Apache Zookeeper 分布式集群

    规划: 三台物理服务器就形成了(法定人数).对于高可用性集群,您可以使用高于3的任何奇数.例如,如果设置5台服务器,则集群可以处理两个故障节点等. 物理服务器需要开启的端口 2888 , 3888 和 ...

  2. eclipse中查看java源代码设置方法

    众所周知,第一次查看class文件时,eclipse会给个界面选择添加源代码路径.   但是,如果我要换源代码路径了怎么办,eclipse也不会提示了.那就使用手动的吧       方法1: 使用ec ...

  3. 两个栈来实现一个队列的C++代码

    利用两个栈来实现一个队列, 这个问题非经常见.  最关键的是要有好的思路, 至于实现, 那是非常easy的事情了. 在本文中, 也想说说自己的思路, 可是. 我认为用代码来表述思路更符合我的习惯. 也 ...

  4. QTreeWidget 的用法

    Qt QTreeWidget 新建一个Qt Widgets Application,拖拽一个Tree Widget 到 ui 界面上,最后实现的效果如下: 添加代码 //test.h //在头文件里添 ...

  5. 《JavaScript》——DOM

    DOM (Document Object Model) 即文档对象模型, 针对 HTML 和 XML 文档的 API (应用程序接口) .DOM 描绘了一个层次化的节点树,执行开发者加入.移除和改动页 ...

  6. PowerBuilder -- 指定重复的列不显示

  7. Java&amp;Xml教程(十)XML作为属性文件使用

    我们一般会将Java应用的配置參数保存在属性文件里.Java应用的属性文件能够是一个正常的基于key-value对,以properties为扩展名的文件.也能够是XML文件. 在本案例中.將会向大家介 ...

  8. mysql的分库分表

    1 什么是分库分表 这里讨论的情况是一台机器上对应一个数据库. 分库的对象是表,分表的对象是行.分库是说把属于同一个模块的相关性很高的表放在同一个数据库中.分表是说把同一个表的的行分成多个子表,把各个 ...

  9. 子串的索引 str.index(sub) sub必须存在

    ii.lstrip(' ')[0:2]=='//' ii.lstrip(' ').index('//')==0

  10. PHP 关于路径的问题

    <?php var_dump(basename(__FILE__)); //返回当前文件/文件夹的的文件名/目录名 var_dump(dirname(__FILE__)); //返回当前文件/文 ...