C. Hard Process

题目连接:

http://www.codeforces.com/contest/660/problem/C

Description

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.

Sample Input

7 1

1 0 0 1 1 0 1

Sample Output

4

1 0 0 1 1 1 1

Hint

题意

你有n个非0就是1的数字,你可以修改最多k个,使得0变成1

然后问你修改之后,最长的连续1的串是多长?

题解:

维护一个前缀0的个数

然后对于每个位置,直接暴力二分就好了,二分这个位置最远能够延展到哪儿

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
int n,k;
int a[maxn],sum[maxn];
int main()
{
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]=1-a[i];
}
for(int i=1;i<=n;i++)
sum[i]=sum[i-1]+a[i];
int ans1=0,ans2=0;
for(int i=1;i<=n;i++)
{
int l = i,r = n,ans=0;
while(l<=r)
{
int mid=(l+r)/2;
if(sum[mid]-sum[i-1]>k)r=mid-1;
else l=mid+1,ans=mid-i+1;
}
if(ans>ans1)
{
ans1=ans;
ans2=i;
}
}
cout<<ans1<<endl;
for(int i=ans2;i<=n;i++)
{
if(ans1==0)break;
if(a[i]==1)a[i]=0;
if(a[i]==0)ans1--;
}
for(int i=1;i<=n;i++)
cout<<1-a[i]<<" ";
}

Educational Codeforces Round 11 C. Hard Process 二分的更多相关文章

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

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

  2. Educational Codeforces Round 11——C. Hard Process(YY)

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

  3. Educational Codeforces Round 11

    A. Co-prime Array http://codeforces.com/contest/660/problem/A 题意:给出一段序列,插进一些数,使新的数列两两成互质数,求插最少的个数,并输 ...

  4. Educational Codeforces Round 21 D.Array Division(二分)

    D. Array Division time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...

  5. Educational Codeforces Round 26 F. Prefix Sums 二分,组合数

    题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...

  6. Educational Codeforces Round 11 E. Different Subsets For All Tuples 动态规划

    E. Different Subsets For All Tuples 题目连接: http://www.codeforces.com/contest/660/problem/E Descriptio ...

  7. Educational Codeforces Round 11 D. Number of Parallelograms 暴力

    D. Number of Parallelograms 题目连接: http://www.codeforces.com/contest/660/problem/D Description You ar ...

  8. Educational Codeforces Round 11 B. Seating On Bus 水题

    B. Seating On Bus 题目连接: http://www.codeforces.com/contest/660/problem/B Description Consider 2n rows ...

  9. Educational Codeforces Round 11 A. Co-prime Array 水题

    A. Co-prime Array 题目连接: http://www.codeforces.com/contest/660/problem/A Description You are given an ...

随机推荐

  1. FastStoneCapture(FSCapture)录屏、剪辑教程

    FastStoneCapture软件编辑视频的使用方法: http://www.tudou.com/programs/view/2eD-s5HP1xw/

  2. CentOS测网速

    当发现上网速度变慢时,人们通常会先首先测试自己的电脑到网络服务提供商(通常被称为"最后一公里")的网络连接速度.在可用于测试宽带速度的网站中,Speedtest.net也许是使用最 ...

  3. 前端nginx时,让后端tomcat记录真实IP【转】

    对于nginx+tomcat这种架构,如果后端tomcat配置保持默认,那么tomcat的访问日志里,记录的就是前端nginx的IP地址,而不是真实的访问IP.因此,需要对nginx.tomcat做如 ...

  4. [会装]Hive安装(基于mysql数据库)

    环境信息:Mac 安装步骤: 1. 下载hive组件(我选择的是社区的2.0.1版本) http://apache.mirror.globo.tech/hive/hive-2.0.1/ 2. 下载my ...

  5. Spring Cloud Feign 输出日志

    还需要在application 文件中配置: #feign调用日志输出logging.level.cn.XXX=DEBUG Logger.Level下面有几种级别. BASIC : 只输出 请求URL ...

  6. [ python ] 作业:选课系统

    功能代码实现源地址:https://www.cnblogs.com/lianzhilei/p/5832691.html    如有侵权,立即删除 本文主要是分析 选课系统 实现思路及上面代码的实现过程 ...

  7. centos6.5 安装、启动vnc

    一.安装vnc 1.确保当前账号是root2.查看本机是否已经安装vncserver rpm -qa|grep tigervnc 3.安装vncserver yum -y install tigerv ...

  8. 51Nod 1352 集合计数(扩展欧几里德)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1352 题目大意: 给出N个固定集合{1,N},{2,N-1} ...

  9. HDU 3001 Travelling(状态压缩DP+三进制)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3001 题目大意:有n个城市,m条路,每条路都有一定的花费,可以从任意城市出发,每个城市不能经过两次以上 ...

  10. HDU 2822

    Dogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...