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).
InputThe 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.
OutputOn 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 input7 1output
1 0 0 1 1 0 1
4input
1 0 0 1 1 1 1
10 2output
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[1000000]; int main (void) { int n,k,s=0,e=0,sum=0,len=0; cin>>n>>k; for(int i = 1; i<=n; i++) { scanf("%d",&a[i]); sum += (a[i] == 0); while(sum > k) { sum -= (a[++s] == 0); } if(len < i - s) { e = i; len = i - s; } } cout<<len<<endl; for(int i = 1; i<=n; i++) if(e>= i && i> e - len ) { printf("1 "); } else { printf("%d ",a[i]); } return 0; }View Code
- motto3
- powerdesgion连接线修改为折线
- 静态链表的实现
- 病案同步状态修改
- 结对编程
- 可执行程序的装载
- 比较字符串是否相等
- 《程序集与命名空间》
- 【使用JSOUP实现网络爬虫】修改数据-设置元素的文本内容
- 为了学好linux 特此写下决心书!
- 代码篇——EasyUI中DataGrid选中多行提交和删除
- leetcode---Same Tree
- 10019---Installing the Accelerator Extension Set
- powerdesigner设置对象颜色
- 【Android基础】四种点击事件
- Excel分数、小数、身份证的录入
- 过河问题-狼羊人菜
- linux进程及作业管理的四大长老-htop/ dstat /top/ ps
- codeforces #313 div1 B
- PowerDesigner如何将消失的工具栏显示出来