Hard Process

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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
  1. 7 1 1 0 0 1 1 0 1
Output
  1. 4 1 0 0 1 1 1 1
Input
  1. 10 2 1 0 0 1 0 1 0 1 0 1
Output
  1. 5 1 0 0 1 1 1 1 1 0 1
    题解:让改变k个数,使序列连续1的个数最大,我们可以求0的前缀和,然后通过二分来找位置;我竟然用暴力超时了好长时间。。。
    二分:
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. using namespace std;
  7. const int INF = 0x3f3f3f3f;
  8. #define mem(x,y) memset(x,y,sizeof(x))
  9. const int MAXN = ;
  10. int num[MAXN];
  11. int a[MAXN];
  12. int L, n, k;
  13.  
  14. int js(int x){
  15. for(int i = ; i + x <= n; i++){
  16. if(num[i + x] - num[i] <= k){
  17. L = i + ;
  18. return true;
  19. }
  20. }
  21. return false;
  22. }
  23. int erfen(int l, int r){
  24. int mid, ans;
  25. while(l <= r){
  26. mid = (l + r) >> ;
  27. if(js(mid)){
  28. ans = mid;
  29. l = mid + ;
  30. }
  31. else
  32. r = mid - ;
  33. }
  34. return ans;
  35. }
  36. int main(){
  37. while(~scanf("%d%d",&n, &k)){
  38. int temp;
  39. memset(num, , sizeof(num));
  40. for(int i = ; i <= n; i++){
  41. scanf("%d", a + i);
  42. num[i] = num[i - ] + (a[i] == );
  43. }
  44. int ans = erfen(,n);
  45. for(int i = L; i < L + ans; i++){
  46. a[i] = ;
  47. }
  48. printf("%d\n", ans);
  49. for(int i = ; i <= n; i++){
  50. if(i != )printf(" ");
  51. printf("%d",a[i]);
  52. }puts("");
  53. }
  54. return ;
  55. }

暴力超时:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. using namespace std;
  7. const int INF = 0x3f3f3f3f;
  8. #define mem(x,y) memset(x,y,sizeof(x))
  9. const int MAXN = ;
  10. int num[MAXN];
  11. int pos[MAXN];
  12. int p[MAXN];
  13. int main(){
  14. int n, k;
  15. while(~scanf("%d%d",&n, &k)){
  16. int gg = ;
  17. for(int i = ; i < n; i++){
  18. scanf("%d", num + i);
  19. if(num[i] == )gg = ;
  20. }
  21. if(!gg){
  22. printf("%d\n",k);
  23. for(int i = ; i < k; i++){
  24. if(i)printf(" ");
  25. printf("");
  26. }
  27. for(int i = k; i < n; i++){
  28. if(i)printf(" ");
  29. printf("");
  30. }puts("");
  31. continue;
  32. }
  33. int kg = , ans = , temp = , cnt = , tp = ;
  34. for(int i = ; i < n; i++){
  35. if(kg == && num[i] == ){
  36. temp = ;
  37. kg = ;
  38. cnt = ;
  39. for(int j = i; j < n; j++){
  40.  
  41. if(num[j] == ){
  42. temp++;
  43. }
  44. else{
  45. if(cnt + > k)break;
  46. pos[cnt++] = j;
  47. temp++;
  48. }
  49. }
  50. int j = i;
  51. while(cnt < k && j > ){
  52. pos[cnt++] = --j;
  53. temp++;
  54. }
  55.  
  56. if(ans < temp){
  57. ans = temp;
  58. tp = cnt;
  59. for(int j = ; j < tp; j++){
  60. p[j] = pos[j];
  61. }
  62. }
  63. }
  64. if(num[i] == )kg = ;
  65. }
  66. for(int i = ; i < tp; i++){
  67. // printf("%d ",p[i]);
  68. num[p[i]] = ;
  69. }//puts("");
  70. printf("%d\n", ans);
  71. for(int i = ; i < n; i++){
  72. if(i)printf(" ");
  73. printf("%d",num[i]);
  74. }
  75. puts("");
  76. }
  77. return ;
  78. }

Hard Process(二分)的更多相关文章

  1. hdu 3433 A Task Process 二分+dp

    A Task Process Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  2. Educational Codeforces Round 11 C. Hard Process 二分

    C. Hard Process 题目连接: http://www.codeforces.com/contest/660/problem/C Description You are given an a ...

  3. codeforces 660C C. Hard Process(二分)

    题目链接: C. Hard Process time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. Codeforces 660C - Hard Process - [二分+DP]

    题目链接:http://codeforces.com/problemset/problem/660/C 题意: 给你一个长度为 $n$ 的 $01$ 串 $a$,记 $f(a)$ 表示其中最长的一段连 ...

  5. hdu3433A Task Process( 二分dp)

    链接 二分时间,在时间内dp[i][j]表示截止到第i个人已经做了j个A最多还能做多少个B #include <iostream> #include<cstdio> #incl ...

  6. 二分+DP HDU 3433 A Task Process

    HDU 3433 A Task Process Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  7. HDU 3433 (DP + 二分) A Task Process

    题意: 有n个员工,每个员工完成一件A任务和一件B任务的时间给出,问要完成x件A任务y件B任务所需的最短时间是多少 思路: DP + 二分我也是第一次见到,这个我只能说太难想了,根本想不到. dp[i ...

  8. Educational Codeforces Round 11 C. Hard Process 前缀和+二分

    题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...

  9. hdu 3433 A Task Process(dp+二分)

    题目链接 题意:n个人, 要完成a个x任务, b个y任务. 求,最短的时间 思路:由于时间较大,用 二分来找时间. dp[i][j]表示 i个人完成j个x任务, 最多能完成的y任务个数 这个题 不是很 ...

随机推荐

  1. IOS Main函数

    int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSS ...

  2. python学习之路-4 内置函数和装饰器

    本篇涉及内容 内置函数 装饰器 内置函数 callable()   判断对象是否可以被调用,返回一个布尔值 1 2 3 4 5 6 7 8 9 10 11 num = 10 print(callabl ...

  3. hdoj 3400 三分

    两次三分 #include <iostream> #include <cstdio> #include <cstring> #include <cmath&g ...

  4. web.cofing(新手必看)

    花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...

  5. .net下载

    1,Http 协议中有专门的指令来告知浏览器, 本次响应的是一个需要下载的文件. 格式如下: Content-Disposition: attachment;filename=filename.ext ...

  6. oracle导出数据显示出现ora-00109或者LRM-00109出错修改办法

    出现这种问题是因为日期格式的问题,调整日期格式后就可以了 改成yyyy-mm-dd的格式就好了

  7. n个数的最大公约、最小公倍数

    #include <cstdio> #include <cstring> using namespace std; #define N 1010 //两个数的最大公约数和最小公 ...

  8. iOS UIWebView 访问https 绕过证书验证的方法

    在文件开始实现  allowsAnyHTTPSCertificateForHost 方法 @implementation NSURLRequest (NSURLRequestWithIgnoreSSL ...

  9. 远程复制 scp命令

    定义 本机为A,用户名为usera,登录远程主机B的为userb,IP为remote_ip 1. 从B 拷贝文件到A机器  用下面的命令 scp userb@remote_ip:remote_path ...

  10. 九、 合成(Composite)模式 --结构模式(Structural Pattern)

    合成模式:有时又叫做部分-整体模式(Part-Whole).合成模式将对象组织到树结构中,可以用来描述整体与部分的关系.合成模式可以使客户端将单纯元素与复合元素同等看待. 合成模式分为安全式和透明式 ...