CodeForces - 660C Hard Process (二分)好题
来源:互联网 发布:美人一笑也倾城网络剧 编辑:程序博客网 时间:2024/06/04 19:26
Description
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).
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.
Sample Input
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
Source
#include<stdio.h>#include<string.h>#include<math.h>#include<set>#include<map>#include<stack>#include<queue>#include<algorithm>#include<iostream>#define INF 0x3f3f3f3f#define ull unsigned long long#define ll long long#define IN __int64#define N 300010#define M 1000000007using namespace std;int a[N],sum[N];int p,n,k;bool judge(int mid){int i,j;for(i=0;i+mid-1<n;i++){if(sum[i+mid-1]-sum[i-1]+k>=mid){p=i;return true;}}return false;}int main(){int i,j;while(scanf("%d%d",&n,&k)!=EOF){memset(sum,0,sizeof(sum));for(i=0;i<n;i++){scanf("%d",&a[i]);sum[i]=sum[i-1]+a[i];}int l=0,r=n+1,mid;p=-1;while(l<r-1)//枚举最长的1的个数 {mid=(l+r)/2;if(judge(mid))l=mid;elser=mid;}printf("%d\n",l);for(i=0;i<p;i++)printf("%d ",a[i]);for(i=p;i<p+l;i++)printf("1 ");for(i=max(p+l,0);i<n;i++)printf("%d ",a[i]);printf("\n");}return 0;}
- CodeForces - 660C Hard Process (二分)好题
- Codeforces 660C Hard Process【二分】经典题!好题!
- Codeforces--660A--Hard Process(二分好题)
- Codeforces 660C Hard Process 【二分】
- 【CodeForces】660C - Hard Process(二分,尺取法)
- Codeforces 660C Hard Process【二分 Or 尺取】
- CodeForces 660C Hard Process (队列)
- CodeForces 660C Hard Process
- codeforces 660C Hard Process
- codeforces 660C Hard Process
- Codeforces 660C-Hard Process【尺取法经典练习】
- Educational Codeforces Round 11 C. Hard Process(尺取 二分 区间天数最多
- CodeForces 688C NP-Hard Problem(二分图判断)
- CF_660C Hard Process(二分)
- Hard Process 二分搜索
- Hard Process CodeForces
- CF#660 C. Hard Process(尺取法)
- Educational Codeforces Round 11——C. Hard Process(YY)
- ListView的优化
- 故事讲解:我是一个线程
- 求解逆序数问题
- POJ3070
- HashMap的indexFor方法
- CodeForces - 660C Hard Process (二分)好题
- POJ3233
- 模拟器综合症
- myid文件缺失导致zookeeper无法启动(myid file is missing)
- 操作系统
- leetcode——18——4Sum
- PHP_CodeSniffer的下载和使用
- 读《杀人之门》
- 使用 Sublime + PlantUML 高效地画图