CodeForces - 660C Hard Process (二分)好题

133 篇文章 0 订阅
106 篇文章 0 订阅
CodeForces - 660C
Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

 Status

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

Input
7 1
1 0 0 1 1 0 1
Output
4
1 0 0 1 1 1 1
Input
10 2
1 0 0 1 0 1 0 1 0 1
Output
5
1 0 0 1 1 1 1 1 0 1

Source

//题意:输入n,k,接着下面一行输入n个数(0或1)
表示给你一个长度为n的序列,让你改变其中的k个0,使得这个序列中连续的1的长度达到最长。输出最长长度并输出改变后的序列。
//思路:
二分法枚举1的最长长度,逐个判断,直到找到最长的连续的1的序列,具体看代码。
#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 1000000007
using 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;
			else
				r=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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值