Educational Codeforces Round 11C. Hard Process two pointer
地址: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).
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.
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.
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
思路:一开始我用的是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 ;
}
最新文章
- js数组方法
- share
- charles 之 ssl proxy 设置(https抓包)
- wordpress woodstock主题导入demo xml文件 execution time out
- CART(分类回归树)
- 解决Linux CentOS中cp -f 复制强制覆盖的命令无效的方法
- Hibernate的ORM原理和实现
- git制作增量包用于更新代码
- ASP.NET反射(转载)
- 8、面向对象以及winform的简单运用(事件与winform入门)
- android 蓝牙设备监听广播
- ZOJ 1202 Divide and Count
- 【caffe-windows】 caffe-master 之 classfication_demo.m 超详细分析
- Hibernate框架增删改查
- Angularjs循环二维数组
- Android 常用知识点
- win10系统磁盘占用率高的解决方法,占用100%的问题
- NPM 模块收集
- Python Flask装饰器登录验证
- Nios II uCLinux/Linux启动分析
热门文章
- Expectation Maximization(EM)算法note
- 安装loadrunner时出现”命令行选项语法错误键入命令 \?获得帮助“的解决方法
- Anaconda2+Theano 安装过程中的所有的坑。。。
- Otter双A同步搭建入门教程
- 【转】CStdioFile UNICODE编译 英文系统下读取中文汉字乱码解决
- DotNet软件开发框架
- 【BZOJ1576】[Usaco2009 Jan]安全路经Travel 最短路+并查集
- 【转】 JS实现HTML标签转义及反转义
- Minecraft Forge编程入门二 “工艺和食谱”
- 简单工厂模式设计(java反射机制改进)