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 ;
}
Educational Codeforces Round 11C. Hard Process two pointer的更多相关文章
- [Educational Codeforces Round 16]E. Generate a String
		
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
 - [Educational Codeforces Round 16]D. Two Arithmetic Progressions
		
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
 - [Educational Codeforces Round 16]C. Magic Odd Square
		
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
 - [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 ...
 - [Educational Codeforces Round 16]A. King Moves
		
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
 - Educational Codeforces Round 6 C. Pearls in a Row
		
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
 - Educational Codeforces Round 9
		
Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...
 - Educational Codeforces Round 37
		
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
 - 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 ...
 
随机推荐
- Meter and pixel units in a box2d game - LibGDX
			
http://pimentoso.blogspot.com/2013/01/meter-and-pixel-units-in-box2d-game.html 需FQ ————————————————— ...
 - DevelopmentValue
			
DevelopmentValue mysql为utf8为什么网页返回数据及写入mysql数据库均为乱码? eclipse运行配了maven之后,创建包也弹出这个错误,每次都弹 c语言怎么建立txt文件 ...
 - 第一百四十七节,封装库--JavaScript,滑动导航
			
JavaScript,封装库--滑动导航 效果图 html <!--滑动导航--> <div id="nav"> <ul class="ab ...
 - CodeSmith自动生成代码使用
			
官网地址:http://www.codesmithtools.com/ CodeSmith开发系列资料总结 http://terrylee.cnblogs.com/archive/2005/12/28 ...
 - 【转】VC++计算当前时间点间隔N天的时间(不使用CTimeSpan类)
			
转自:http://blog.csdn.net/fzuim/article/details/61199351 涉及到有效期的设置,需要计算N天时间间隔的时间. C++ Code 123456789 ...
 - 【转】CStdioFile UNICODE编译 英文系统下读取中文汉字乱码解决
			
转载出处:http://www.cnblogs.com/ct0421/p/3242418.html 函数原形为:char *setlocale( int category, const char *l ...
 - iOS   -转载-开发之个人开发者账号转公司开发者账号
			
ps : 个人开发者账号升级公司开发者账号的话需要账号开启双重认证,没有开启的话需要开启(不然走到可以升级的那步的话,点击update升级会提示为了安全起见需要账号开启双双重认证,反正我走到upd ...
 - NSTimer的循环引用
			
在日常开发中想到会引起循环引用的一般比较容易想起的是 1.delegate 2.block 今天要说的就是另外一个,NSTimer 这个比较容易会被忽略掉 简单的说就是创建timer为成员变量的时候t ...
 - MVC结构简介
			
本文编译自J2EE的相关文档.MVC(Model-View-Controller)应用程序结构被用来分析分布式应用程序的特征.这种抽象结构能有助于将应用程序分割成若干逻辑部件,使程序设计变得更加容易. ...
 - NATS源代码之logger目录
			
nats的logger目录文件如下 log.go syslog.go syslog_windows.go 基于golang语言的logger包实现日志功能. Golang的log包短小精悍,可以非常轻 ...