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 11 0 0 1 1 0 1
41 0 0 1 1 1 1
10 21 0 0 1 0 1 0 1 0 1
51 0 0 1 1 1 1 1 0 1
思路:一开始我用的是n^2的算法,一直tle,后来才知道有种算法叫尺取法:就是动态维护一个长度为x的区间,并同时记录起始位置和终点位置。
对这题而言,就是维护含0数为k的0,1区间,记录长度最大值,和起始位置和终点位置;
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cmath> 5 #include <cstring> 6 #include <queue> 7 #include <stack> 8 #include <map> 9 #include <vector> 10 11 #define PI acos((double)-1) 12 #define E exp(double(1)) 13 using namespace std; 14 int a[1000000]; 15 int main (void) 16 { 17 int n,k,s=0,e=0,sum=0,len=0; 18 cin>>n>>k; 19 for(int i = 1; i<=n; i++) 20 { 21 scanf("%d",&a[i]); 22 sum += (a[i] == 0); 23 while(sum > k) 24 { 25 sum -= (a[++s] == 0); 26 } 27 if(len < i - s) 28 { 29 e = i; 30 len = i - s; 31 } 32 } 33 cout<<len<<endl; 34 for(int i = 1; i<=n; i++) 35 if(e>= i && i> e - len ) 36 { 37 printf("1 "); 38 } 39 else 40 { 41 printf("%d ",a[i]); 42 } 43 return 0; 44 }
View Code
转载于:https://www.cnblogs.com/weeping/p/5371900.html
Educational Codeforces Round 11C. Hard Process two pointer相关推荐
- [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 114 (Rated for Div. 2) (A ~ F)全题解
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Educational Codeforces Round 114 (Rated for Div. 2) ...
- Educational Codeforces Round 106 (Rated for Div. 2)(A ~ E)题解(每日训练 Day.16 )
整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 Educational Codeforces Round 106 (Rated for Div. ...
- Educational Codeforces Round 32
http://codeforces.com/contest/888 A Local Extrema[水] [题意]:计算极值点个数 [分析]:除了第一个最后一个外,遇到极值点ans++,包括极大和极小 ...
- Educational Codeforces Round 37 (Rated for Div. 2) 1
Educational Codeforces Round 37 (Rated for Div. 2) A.Water The Garden 题意:Max想给花园浇水.花园可被视为长度为n的花园床,花园 ...
- Educational Codeforces Round 90 (Rated for Div. 2)(A, B, C, D, E)
Educational Codeforces Round 90 (Rated for Div. 2) Donut Shops 思路 分三种情况: a==c/ba == c / ba==c/b这个时候两 ...
- Educational Codeforces Round 89 (Rated for Div. 2)(A, B, C, D)
Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords 思路 题意非常简单,就是得到最多的物品嘛,我们假定a, ...
- Educational Codeforces Round 114 (Rated for Div. 2) D. The Strongest Build 暴力 + bfs
传送门 文章目录 题意: 思路: 题意: 你有nnn个装备槽,每个槽里面有cic_ici个力量加成,对于每个槽只能选一个力量加成,现在给你mmm个力量组合[b1,b2,...,bn][b_1,b_2 ...
- Educational Codeforces Round 17 E. Radio stations cdq分治 + 树状数组
传送门 文章目录 题意 思路: 题意 有nnn个电台,对于每个电台iii有三个参数xi,ri,fix_i,r_i,f_ixi,ri,fi,分别指他们的坐标.作用半径.频率.如果两个电台频率差值在 ...
最新文章
- poj 1961 Period
- List遍历删除注意事项
- 【uoj#209】[UER #6]票数统计 组合数+乱搞
- python生成字符画_通过python将图片生成字符画
- NPOI从数据库中导出数据到Excel
- python之show、hide、slidedonw、slideup方法实例
- 前端H5 使用百度统计进行埋点
- 奥维地图显示no data(-5)解决
- android 智能手机usb驱动程序,华为手机usb驱动下载
- 5.用转换器抽取特征
- Faceswap文档之---使用手册
- uniapp app端根据支付宝qr链接 直接打开支付宝app支付页面
- python中sqrt(4)*sqrt(9)_Python表达式sqrt(4)*sqrt(9)的值为()
- 怎么通过抖音带货赚钱?史上最全面抖音带货攻略
- 读书寄语:所有的遇见都是有原因的,并不只是偶然
- java字符转转长整型_P104 将数字字符串转换成长整型整数 ★★
- 洽洽:百亿路上无“鲜”事
- sqlsrver-常见英语单词释意
- 不断进化中的新型零售 便利蜂力主科技赋能
- JavaScript 循环嵌套案例、while 语句、 do...while 语句、continue 关键字、5 break 关键字
热门文章
- php video标签使用方法,HTML_HTML5 video标签(播放器)学习笔记(一):使用入门,近有在学习html5中video标签(播 - phpStudy...
- android 应用程序分析之settings,Android 应用程序分析之Settings
- smarty中打印php变量,smarty用php标签怎么输出变量
- mysql编辑工具wf_轻松构建自定义WF设计器
- vim编辑器全部删除文件内容
- 【微信小程序企业级开发教程】界面跳转方法总结
- python【力扣LeetCode算法题库】面试题 17.16- 按摩师(DP)
- centos 6.5手动同步ntp时钟_如何安装和配置 Chrony 作为 NTP 客户端? | Linux 中国
- vue打包后element-icon不显示问题解决
- 网络营销外包专员浅析尽管快照不见了网络营销外包仍在继续