地址: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 ;
}

Educational Codeforces Round 11C. Hard Process two pointer的更多相关文章

  1. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  2. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  3. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  4. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  5. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  6. Educational Codeforces Round 6 C. Pearls in a Row

    Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...

  7. Educational Codeforces Round 9

    Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...

  8. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

随机推荐

  1. 【BZOJ】3394: [Usaco2009 Jan]Best Spot 最佳牧场(floyd)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3394 裸的floyd.. #include <cstdio> #include < ...

  2. poj 2503 Babelfish(字典树或着STL)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 35828   Accepted: 15320 Descr ...

  3. jni调用 java.lang.UnsatisfiedLinkError: no segmentor_jni in java.library.path

    改过 LD_LIBRARY_PATH 改过 /etc/ld.so.conf 参考这篇文章 http://blog.csdn.net/zjuylok/article/details/4152559 最后 ...

  4. 在 App Store 三年學到的 13 件事(下)

    博文转载至 http://blog.csdn.net/iunion/article/details/18959801     Steven Shen,曾經寫過一本書,也翻過一本書,開發 iOS app ...

  5. Flutter入门之无状态组件

    Flutter核心理念 flutter组件采用函数式响应框架构建,它的灵感来自于React.它设计的核心思想是组件外构建UI,简单解释一下就是组件鉴于它当前的配置和状态来描述它的视图应该是怎样的,当组 ...

  6. Word Formation

    构词 Word Formation 1.派生Derivation 2.合成Compounding 3.截短Clipping 4.混合Blending 1派生Derivation 1).前缀 除少数英语 ...

  7. win7(iis7)无法加载运行CSS文件的解决方法

    在打开或关闭window功能中的Internet信息服务里的万维网服务=>常见HTTP功能=>静态内容 ,将其选上 即可了,如下图

  8. ubuntu微信

    方法1 – 使用Snap来安装微信 依次在terminal 执行一下命令 sudo apt install snapd snapd-xdg-open sudo snap install electro ...

  9. Spring4学习笔记-AOP(基于配置文件的方式)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://shamrock.blog.51cto.com/2079212/1557743 引 ...

  10. 小程序wxParse插件的使用

    微信小程序输出html内容数据插件wxParse,可以把带html标签的数据输出为微信小程序正常显示的格式,wxParse插件带有演示,也有使用文档说明. 下载地址:https://github.co ...