codeforces660C
Hard Process
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
7 1
1 0 0 1 1 0 1
4
1 0 0 1 1 1 1
10 2
1 0 0 1 0 1 0 1 0 1
5
1 0 0 1 1 1 1 1 0 1 sol:只能把0变成1,而且要让连续的一段尽可能的大,于是可以枚举右端点二分左端点,判断那一段0的个数与K的关系
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,m,a[N],S[N];
int Ans[N];
inline int TwoFind(int l,int r)
{
int Pos=r,ans=r+;;
while(l<=r)
{
int mid=(l+r)>>;
if(S[Pos]-S[mid-]<=m)
{
ans=mid; r=mid-;
}
else l=mid+;
}
return ans;
}
int main()
{
int i,Pos=;
R(n); R(m);
for(i=;i<=n;i++)
{
R(a[i]); S[i]+=S[i-]+(a[i]==);
int oo=TwoFind(,i);
Ans[i]=i-oo+;
if(Ans[i]>Ans[Pos]) Pos=i;
}
Wl(Ans[Pos]);
for(i=Pos;i>=&&m;i--) if(a[i]==) a[i]=,m--;
for(i=;i<=n;i++) W(a[i]);
return ;
}
/*
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
*/
codeforces660C的更多相关文章
随机推荐
- 【阿里云】在 Windows Server 2016 下使用 FileZilla Server 安装搭建 FTP 服务
Windows Server 2016 下使用 FileZilla Server 安装搭建 FTP 服务 一.安装 Filezilla Server 下载最新版本的 Filezilla Server ...
- WebView断网提示
转载请标明出处,维权必究:https://www.cnblogs.com/tangZH/p/9913968.html 重写WebViewClient中的方法,然后WebView.setWebViewC ...
- 自定义HorizontalScrollView的scrollBar
尊重劳动成果,转载请标明出处http://www.cnblogs.com/tangZH/p/8423803.html android滑动组件的scrollBar,看了不是很顺眼,没办法,因为项目需求, ...
- Redis数据库云端最佳技术实践
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由腾讯云数据库 TencentDB发表于云+社区专栏 邹鹏,腾讯高级工程师,腾讯云数据库Redis负责人,多年数据库.网络安全研发经验. ...
- 用微软官方的 HTML Help Workshop制作的CHM文件不显示图片解决办法
制作CHM文档,方便小巧还易于查看,用处自不必多说了,最近想把这个季度所学习的内容全部制作成CHM格式文档,给自己后续用来温故而知新,同时也可以做为后起之秀避坑法宝.但是在生成CHM文档之后发现有些地 ...
- python离线安装包
一.用download命令离线下载包 *.whl , 这个方法好像python3.7以上才能用 那么我的requirement.txt内容就是: django==1.8.11 simplejson= ...
- 【Python 07】汇率兑换1.0-2(基本元素)
1.Python基本元素 (1)缩进:表示代码层次关系(Python中表示程序框架唯一手段) 1个tab或者4个空格 (2)注释:开发者加入的说明信息,不被执行.一个代码块一个注释. # 单行注释(一 ...
- Ambari——大数据平台的搭建利器之进阶篇
前言 本文适合已经初步了解 Ambari 的读者.对 Ambari 的基础知识,以及 Ambari 的安装步骤还不清楚的读者,可以先阅读基础篇文章<Ambari——大数据平台的搭建利器>. ...
- ios自动打包-fastlane 安装、使用、更新和卸载
ios自动打包使用fastlane 1.首先安装xcode 首先检查是否已经安装 Xcode 命令行工具,fastlane 使用 xcodebuild 命令进行打包,运行 xcode-select - ...
- 25 python 初学(socket,socketserver)
参考blog :www.cnblogs.com/yuanchenqi/articles/5692716.html 1. sk = socket.socket() 里面有两个重要的参数,family 和 ...