题干:

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

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

题目大意:

给定一个数组 a,含有 n 个元素。数组 a 中的每个元素要么是 0,要么是 1 。输入一个n,一个k,然后第二行是数组。

让我们假定,数组 a 中,仅由数字 1 组成的连续元素所构成的子分段,其最长的长度为 f(a)。您可以将不超过 k 个 0 更改为 1,使得 f(a) 最大化。

解题报告:

这题做法多样,可以nlogn二分长度然后遍历。还有一种方法是o(n)尺取,但是感觉难写一点?

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define ll long long
using namespace std;
const int MAX = 3e5 + 5;
int n,k;
int a[MAX],sum[MAX];bool ok(int len) {for(int i = 1; i+len-1<=n; i++) {if(sum[i+len-1] - sum[i-1] >= len-k) return 1;}return 0;
}
int main()
{cin>>n>>k;for(int i = 1; i<=n; i++) scanf("%d",a+i),sum[i] = sum[i-1] + a[i];int l = 0,r = n;int mid = (l+r)/2;while(l<r) {mid = (l+r+1)/2;if(ok(mid)) l=mid;else r=mid-1;}printf("%d\n",l);for(int i = 1; i+l-1<=n; i++) {if(sum[i+l-1] - sum[i-1] >= l-k) {for(int j = 1; j<=n; j++) {if(j < i || j > i+l-1) printf("%d",a[j]);else printf("1");if(j != n) putchar(' ');}return 0 ;}}return 0 ;} 

总结:

注意这题是二分求最大值,所以需要mid=(l+r+1)/2调节一下。最后还是l是我们需要的。

【CodeForces - 660C】Hard Process (尺取 或 二分+滑窗,前缀和预处理)相关推荐

  1. 【Gym - 101608G】WiFi Password (区间或,线段树 或 按位处理+尺取 或 二分)

    题干: Just days before the JCPC, your internet service went down. You decided to continue your trainin ...

  2. The Best Vacation CodeForces - 1358D(贪心+尺取)

    You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until ...

  3. *【POJ - 3061】 Subsequence (尺取或二分)

    题干: A sequence of N positive integers (10 < N < 100 000), each of them less than or equal 1000 ...

  4. CodeForces - 660C Hard Process

    题意就是 在这个数串中插入1构成一个最长的连续1的区间串 如何做?我们可以对长度进行二分 从1到n二分长度 然后在对这个长度拿到数串中查看是否能构成最终我们想要的最长匹配串 这里我们可以利用一个前缀和 ...

  5. CodeForces 660C Hard Process

    预处理前缀和+枚举起点+二分终点. #include<cstdio> #include<cstring> #include<cmath> #include<v ...

  6. 【CodeForces - 514D】R2D2 and Droid Army(二分+滑动窗口ST表,或 尺取+单调队列或STLmultiset)

    题干: An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, ..., a ...

  7. CF1547F Array Stabilization (GCD version) st表 + 尺取/二分

    传送门 题意: 思路: 容易发现,我们将所有aaa都除上所有aaa的gcdgcdgcd,实际上就是让你求一个最小的lenlenlen,对于所有iii,gcd(ai,ai+1,...,ai+len−1) ...

  8. Codeforces Round #736 (Div. 2) D. Integers Have Friends ST表gcd + 尺取

    传送门 文章目录 题意: 思路: 题意: 给你一个序列aaa,求一个最长的子序列[l,r][l,r][l,r]满足aimodm=ai+1modm=...=armodma_i\bmod m=a_{i+1 ...

  9. 【尺取或dp】codeforces C. An impassioned circulation of affection

    http://codeforces.com/contest/814/problem/C [题意] 给定一个长度为n的字符串s,一共有q个查询,每个查询给出一个数字m和一个字符ch,你的操作是可以改变字 ...

最新文章

  1. 免费技术直播:唐宇迪带你一节课了解机器学习经典算法
  2. -bash: sz: command not found
  3. sa执行命令方法总结
  4. 200721学习日报
  5. aspx写入mysql_Asp.net用户登陆数据库验证与注册写入数据库
  6. 年轻人的第一套租房?小米成立新公司或涉房屋租赁业务
  7. python编程(一种自定义定时器的写法)
  8. CentOS7 按nodejs和npm的方法
  9. xcode6.1中 symbolicatecrash位置
  10. 拓端tecdat|R语言结合新冠疫情COVID-19对股票价格预测:ARIMA,KNN和神经网络时间序列分析
  11. python二元一次方程组用鸡兔同笼的思路来写编程_《应用二元一次方程组——鸡兔同笼》...
  12. 行业边缘丨中国电科发布“海雀”处理器;中科海微获千万融资;联想发布边缘服务器;风河加入CNCF云原生计算基金会成为银牌会员;...
  13. 已解决At least Windows 8.1 or Windows Server 2012 are required to install Python 3.9.4(64-bit)
  14. drill apache_使用Apache Drill对混合模式数据进行SQL查询
  15. FME在测量平差上面的运用
  16. EM算法从直观到数学理解
  17. 计算机科学技术想象作文,【精华】科技想象作文5篇
  18. mysql 查看时区_mysql 查看及修改时区的方法
  19. 为什么放弃Kafka,选择Pulsar?
  20. 遥感影像镶嵌拼接如何抠图及多源影像补图,如何进行区域替换去云异常区域

热门文章

  1. HDU-6180 Schedule
  2. dft计算傅里叶级数系数_一道国外的DFT性质的题目
  3. 背景图层和普通图层的区别_008Photoshop四赞图层(图层样式)
  4. java类验证和装载顺序_java中类的加载顺序介绍(ClassLoader)
  5. 653B. Bear and Compressing
  6. js菜单自适应的实现
  7. UE4 多个static mesh合并成一个static mesh
  8. Nand Flash Yaffs系统大量写入数据失败问题的解决方法。
  9. 计算机组装与维护模拟测试题三答案,春季高考信息技术模拟题3(计算机组装与维修部分含答案)...
  10. 【转】VTK + QT + VS 编译安装配置