博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Educational Codeforces Round 11 C. Hard Process 二分
阅读量:7093 次
发布时间:2019-06-28

本文共 1545 字,大约阅读时间需要 5 分钟。

C. Hard Process

题目连接:

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 1

1 0 0 1 1 0 1

Sample Output

4

1 0 0 1 1 1 1

Hint

题意

你有n个非0就是1的数字,你可以修改最多k个,使得0变成1

然后问你修改之后,最长的连续1的串是多长?

题解:

维护一个前缀0的个数

然后对于每个位置,直接暴力二分就好了,二分这个位置最远能够延展到哪儿

代码

#include
using namespace std;const int maxn = 1e6;int n,k;int a[maxn],sum[maxn];int main(){ scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); a[i]=1-a[i]; } for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i]; int ans1=0,ans2=0; for(int i=1;i<=n;i++) { int l = i,r = n,ans=0; while(l<=r) { int mid=(l+r)/2; if(sum[mid]-sum[i-1]>k)r=mid-1; else l=mid+1,ans=mid-i+1; } if(ans>ans1) { ans1=ans; ans2=i; } } cout<
<

转载地址:http://ujiql.baihongyu.com/

你可能感兴趣的文章
为CentOS 6.5 配置本地YUM源
查看>>
linux grep命令
查看>>
Memcache知识点梳理
查看>>
1.java用户校验
查看>>
【MySQL】lower_case_table_names参数详解
查看>>
定时任务crond生产实战经验
查看>>
mysql-5.5配置主从 及 主主关系
查看>>
高级文件系统管理
查看>>
磁盘阵列RAID的功能作用介绍
查看>>
安装discuz
查看>>
左值和右值
查看>>
anisble变量二(针对默认收集的信息处理)
查看>>
[大数据行业应用发展前景分析] 阿里潘永花报告:大数据产业将成为新的煤和石油介绍...
查看>>
聊聊spring cloud gateway的streaming-media-types属性
查看>>
dns 搭建和正向逆向解析
查看>>
TCP数据的传输进程
查看>>
实验18 交换机的端口安全
查看>>
Linux学习笔记第四周第二次课(2月27日)
查看>>
通过Nginx使全站页面变灰
查看>>
使用mysqlsla分析Mysql数据库日志
查看>>