首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【CodeForces】660C - Hard Process(二分,尺取法)

【CodeForces】660C - Hard Process(二分,尺取法)

作者头像
FishWang
发布2025-08-26 20:30:18
发布2025-08-26 20:30:18
16200
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

C. Hard Process

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples

input

代码语言:javascript
代码运行次数:0
运行
复制
7 1
1 0 0 1 1 0 1

output

代码语言:javascript
代码运行次数:0
运行
复制
4
1 0 0 1 1 1 1

input

代码语言:javascript
代码运行次数:0
运行
复制
10 2
1 0 0 1 0 1 0 1 0 1

output

代码语言:javascript
代码运行次数:0
运行
复制
5
1 0 0 1 1 1 1 1 0 1

很好的一个二分法的题。

首先用一个sum数组储存到当前节点0的个数,然后用一个 for 循环从第一个数(不包括)开始尺取,依次更新结果就行了。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int num[300000+11];
int sum[300000+11] = {0};		//在当前节点0的个数 
int main()
{
	int n,k;
	int pos;
	int l,st,endd;		//长度,起点,终点(闭区间) 
	while (~scanf ("%d %d",&n,&k))
	{
		memset (sum,0,sizeof (sum));
		for (int i = 0 ; i < n ; i++)
		{
			scanf ("%d",&num[i]);
			if (num[i])
				sum[i] = sum[i-1];
			else
				sum[i] = sum[i-1] + 1;
		}
		//首先先计算从第一个数开始的结果
		pos = upper_bound (sum , sum + n , k) - 1 - sum;
		l = pos + 1;
		st = 0;
		endd = pos;
		//然后计算左开右闭区间的最优解
		for (int i = 0 ; i < n ; i++)
		{
			pos = upper_bound (sum , sum + n , sum[i] + k) - 1 - sum;
			if (pos - i > l)
			{
				l = pos - i;
				st = i + 1;
				endd = pos;
			}
		}
		//最后输出
		for (int i = st ; i <= endd ; i++)
			num[i] = 1;
		printf ("%d\n",l);
		for (int i = 0 ; i < n ; i++)
			printf ("%d ",num[i]);
		printf ("\n");
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档