Codeforces 660C-Hard Process【尺取法经典练习】

本是古典 何须时尚 2022-08-21 12:56 248阅读 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 a**i (0 ≤ a**i ≤ 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 a**j — the elements of the array a after the changes.

If there are multiple answers, you can print any one of them.

Examples

input

  1. 7 1
  2. 1 0 0 1 1 0 1

output

  1. 4
  2. 1 0 0 1 1 1 1

input

  1. 10 2
  2. 1 0 0 1 0 1 0 1 0 1

output

  1. 5
  2. 1 0 0 1 1 1 1 1 0 1
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<algorithm>
  6. using namespace std;
  7. int num[300000+100];
  8. int main()
  9. {
  10. int n,k;
  11. while(scanf("%d%d",&n,&k)!=EOF)
  12. {
  13. for(int i=1;i<=n;i++)
  14. {
  15. scanf("%d",&num[i]);
  16. }
  17. int r=1,l=1,zr=0,ans=0,ansl=1,ansr=0;
  18. while(r<=n)
  19. {
  20. while(r<=n&&zr<=k)
  21. {
  22. if(num[r]==0)
  23. {
  24. if(zr==k)
  25. break;
  26. else
  27. {
  28. zr++;
  29. }
  30. }
  31. r++;
  32. }
  33. if(r-l>ans)
  34. {
  35. ansr=r-1;
  36. ansl=l;
  37. ans=r-l;
  38. }
  39. while(l<=n&&num[l])
  40. l++;
  41. l++,zr--;
  42. }
  43. printf("%d\n",ans);
  44. for(int i=1;i<=n;i++)
  45. {
  46. if(i>=ansl&&i<=ansr)
  47. printf("1 ");
  48. else
  49. printf("%d ",num[i]);
  50. }
  51. printf("\n");
  52. }
  53. return 0;
  54. }

发表评论

表情:
评论列表 (有 0 条评论,248人围观)

还没有评论,来说两句吧...

相关阅读

    相关 总结

    一般地,当我们要去枚举满足条件的区间权值的区间,可以用遍历双指针 l 和 r 的做法去枚举第一个满足条件的区间,然后去维护其他数据,这样的做法就是尺取法,它的复杂度为O(n)

    相关 模板

      题目描述 博览馆正在展出由世上最佳的 M 位画家所画的图画。 wangjy想到博览馆去看这几位大师的作品。 可是,那里的博览馆有一个很奇怪的规定,就是在购买门票

    相关 算法--

    尺取法 尺取法通常是指对数组保存一对下标(起点,终点),然后根据实际情况交替推进两个端点直到得出答案的方法,这种操作很像是尺取虫爬行的方式故得名。 我们先来看看POJ的

    相关 Pie————二分+练习

    给你n个蛋糕的半径,你有m个朋友,所以总共有m+1个人,现在要分蛋糕,要求每个人分到的大小都是一样的,且每个人的蛋糕都是从一块上切割下来的(不能是2个不同的蛋糕拼起来的),现在