codeforces 660C C. Hard Process(二分)
2016-04-09 01:16
483 查看
题目链接:C. Hard Processtime limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou 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.Examplesinput
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou 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.Examplesinput
7 1 1 0 0 1 1 0 1output
4 1 0 0 1 1 1 1input
10 2 1 0 0 1 0 1 0 1 0 1output
5 1 0 0 1 1 1 1 1 0 1 题意: 最多把k个0变成1,变完后连续的最长的全是1的串的长度是多少,并且输出最后得串; 思路: 用一个数组记录当前位一共有多少个0,暴力枚举最长串的最后一位,二分查找最长串的第一个1的位置;更新结果并记录好最长串的开始和结束位置,最后再输出就好啦; AC代码:
/* 2014300227 660C - 5 GNU C++11 Accepted 93 ms 4388 KB */ #include <bits/stdc++.h> using namespace std; const int N=3e5+4; typedef long long ll; const double PI=acos(-1.0); int n,a ,k,b ; int check(int x,int y) { if(b[y]-b[x-1]<=k)return 1; return 0; } int bis(int num) { int l=1,r=num,mid; while(l<=r) { mid=(l+r)>>1; if(check(mid,num))r=mid-1; else l=mid+1; } return r+1; } int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); if(!a[i])b[i]=b[i-1]+1; else b[i]=b[i-1]; } int ans=0,l=0,r=0; for(int i=1;i<=n;i++) { int fx=bis(i); if(i-fx+1>ans) { ans=i-fx+1; l=fx; r=i; } } printf("%d\n",ans); for(int i=1;i<=n;i++) { if(i>=l&&i<=r) { printf("1 "); } else printf("%d ",a[i]); } return 0; }
相关文章推荐
- 20160409
- ORA-19698: /home/xxx/xxx/redo01.log is from different database
- Js实现类似PHP里trim方法
- 把一个Color转为html里所用的颜色代码
- 利用缓冲区溢出越过if校验
- CentOS 7.0安装Mysql
- linux 下记录ssh的操作日志
- Selector 失效
- x86汇编--(七)仿高级语言之for循环嵌套打印字符串
- zookeeper和kafka实践
- 笔记本自带 WiFi 功能
- LeetCode 62. Unique Paths
- CF 628A --- Tennis Tournament --- 水题
- codeforces 660B B. Seating On Bus(模拟)
- Android Paint之 setXfermode PorterDuffXfermode 讲解
- Android Studio 导入 Vitamio及其笔记
- Particle 离子系统
- Android的启动过程
- 跟我一起学习MySQL技术内幕(第五版):(第二章学习日记2)
- Git教程总结,一些日常用到的基本方法