地址:http://codeforces.com/contest/660/problem/C

题目:

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

思路:一开始我用的是n^2的算法,一直tle,后来才知道有种算法叫尺取法:就是动态维护一个长度为x的区间,并同时记录起始位置和终点位置。

  对这题而言,就是维护含0数为k的0,1区间,记录长度最大值,和起始位置和终点位置;

 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <vector> #define PI acos((double)-1)
#define E exp(double(1))
using namespace std;
int a[];
int main (void)
{
int n,k,s=,e=,sum=,len=;
cin>>n>>k;
for(int i = ; i<=n; i++)
{
scanf("%d",&a[i]);
sum += (a[i] == );
while(sum > k)
{
sum -= (a[++s] == );
}
if(len < i - s)
{
e = i;
len = i - s;
}
}
cout<<len<<endl;
for(int i = ; i<=n; i++)
if(e>= i && i> e - len )
{
printf("1 ");
}
else
{
printf("%d ",a[i]);
}
return ;
}

最新文章

  1. js数组方法
  2. share
  3. charles 之 ssl proxy 设置(https抓包)
  4. wordpress woodstock主题导入demo xml文件 execution time out
  5. CART(分类回归树)
  6. 解决Linux CentOS中cp -f 复制强制覆盖的命令无效的方法
  7. Hibernate的ORM原理和实现
  8. git制作增量包用于更新代码
  9. ASP.NET反射(转载)
  10. 8、面向对象以及winform的简单运用(事件与winform入门)
  11. android 蓝牙设备监听广播
  12. ZOJ 1202 Divide and Count
  13. 【caffe-windows】 caffe-master 之 classfication_demo.m 超详细分析
  14. Hibernate框架增删改查
  15. Angularjs循环二维数组
  16. Android 常用知识点
  17. win10系统磁盘占用率高的解决方法,占用100%的问题
  18. NPM 模块收集
  19. Python Flask装饰器登录验证
  20. Nios II uCLinux/Linux启动分析

热门文章

  1. Expectation Maximization(EM)算法note
  2. 安装loadrunner时出现”命令行选项语法错误键入命令 \?获得帮助“的解决方法
  3. Anaconda2+Theano 安装过程中的所有的坑。。。
  4. Otter双A同步搭建入门教程
  5. 【转】CStdioFile UNICODE编译 英文系统下读取中文汉字乱码解决
  6. DotNet软件开发框架
  7. 【BZOJ1576】[Usaco2009 Jan]安全路经Travel 最短路+并查集
  8. 【转】 JS实现HTML标签转义及反转义
  9. Minecraft Forge编程入门二 “工艺和食谱”
  10. 简单工厂模式设计(java反射机制改进)