题目链接:

http://codeforces.com/problemset/problem/660/C

题意:

给定0.1组成的数组,可以改变k个0使其为1,问最终可以得到的连续的1的最大长度。

分析:

很容易想到二分答案的做法,

二分长度,然后找是否存在满足题意的区间。

还可以用尺取法,这样在O(n)时间负责度内就可以完成,但是个人感觉写起来没有二分直观。。

代码:

二分:

  1. #include<cstdio>
  2. #include<cmath>
  3. #include<iostream>
  4. using namespace std;
  5. const int maxn = 300000 + 5;
  6. int a[maxn];
  7. int t[maxn];
  8. int n, k;
  9. int f1 = -1;
  10. bool judge(int p)
  11. {
  12. for(int i = 0; i + p - 1< n; i++){
  13. if(t[i + p - 1] - t[i - 1] + k >= p) {
  14. f1 = i;
  15. return true;
  16. }
  17. }
  18. return false;
  19. }
  20. int main (void)
  21. {
  22. scanf("%d%d", &n, &k);
  23. for(int i = 0; i < n; i++){
  24. scanf("%d", &a[i]);
  25. t[i] = t[i - 1] + (a[i] == 1);
  26. }
  27. int l = 0, r = n + 1;
  28. while(l < r - 1){
  29. int mid = (l + r) / 2;
  30. if(judge(mid)) l = mid;
  31. else r = mid;
  32. }
  33. printf("%d\n", l);
  34. for(int i = 0; i < f1; i++){
  35. printf("%d ", a[i]);
  36. }
  37. for(int i = f1; i < f1 + l; i++)
  38. printf("1 ");
  39. for(int i = max(f1 + l, 0); i < n; i++){
  40. printf("%d ", a[i]);
  41. }
  42. return 0;
  43. }

尺取法:(two pointers)

  1. #include<cstdio>
  2. #include<cmath>
  3. #include<iostream>
  4. using namespace std;
  5. const int maxn = 300000 + 5;
  6. int a[maxn], t[maxn];
  7. int n, k;
  8. int main (void)
  9. {
  10. scanf("%d%d", &n, &k);
  11. for(int i = 0; i < n; i++){
  12. scanf("%d", &a[i]);
  13. }
  14. int r = 0, cnt = 1, ans = 0, MAX = 0;
  15. int f1 = -1, f2 = -1;
  16. for(int i = 0; i < n; i++){
  17. if(a[i - 1] == 0){
  18. cnt--;
  19. while(cnt <= k && r < n){
  20. if(a[r] == 1){
  21. r++;
  22. }else{
  23. if(cnt == k) {break;}
  24. cnt++;
  25. r++;
  26. }
  27. }
  28. ans = r - i ;
  29. }else ans--;
  30. if(ans > MAX){
  31. MAX = ans;
  32. f1 = i;
  33. f2 = r - 1;
  34. }
  35. }
  36. cout<<MAX<<endl;
  37. if(k == 0){
  38. for(int i = 0; i < n; i++) cout<<a[i]<<' ';
  39. return 0;
  40. }
  41. for(int i = 0; i < f1; i++) cout<<a[i]<<' ';
  42. for(int i = f1; i <= f2; i++) cout<<1<<' ';
  43. for(int i = f2 + 1; i < n; i++) cout<<a[i]<<' ';
  44. return 0;
  45. }

Codeforces 660C Hard Process【二分 Or 尺取】的更多相关文章

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

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

  2. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot(二分或者尺取)

    题目哦 题意:给出一个序列,序列有四个字母组成,U:y+1,D:y-1 , L:x-1 , R:x+1;   这是规则 . 给出(x,y) 问可不可以经过最小的变化这个序列可以由(0,0) 变到(x, ...

  3. POJ-3061 Subsequence 二分或尺取

    题面 题意:给你一个长度为n(n<100000)的数组,让你找到一个最短的连续子序列,使得子序列的和>=m  (m<1e9) 题解: 1 显然我们我们可以二分答案,然后利用前缀和判断 ...

  4. B. Complete the Word(Codeforces Round #372 (Div. 2)) 尺取大法

    B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. Codeforces - 1191E - Tokitsukaze and Duel - 博弈论 - 尺取

    https://codeforc.es/contest/1191/problem/E 参考自:http://www.mamicode.com/info-detail-2726030.html 和官方题 ...

  6. Codeforces 660C Hard Process(尺取法)

    题目大概说给一个由01组成的序列,要求最多把k个0改成1使得连续的1的个数最多,输出一种方案. 和CF 676C相似. #include<cstdio> #include<algor ...

  7. POJ:3579-Median(二分+尺取寻找中位数)

    Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9201 Accepted: 3209 Description Gi ...

  8. poj 3579 Median 二分套二分 或 二分加尺取

    Median Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5118   Accepted: 1641 Descriptio ...

  9. codeforces 660C Hard Process

    维护一个左右区间指针就可以. #include<cstdio> #include<cstring> #include<iostream> #include<q ...

随机推荐

  1. 【程小白】Java基本特性

    一.StringBuffer.StringBuilder的区别 StringBuffer是线程安全的,StringBuilder是线程不安全的.所以以后在单线程中,如果涉及大量字符串操作,还是用Str ...

  2. java将一个List赋值给另一个List的4种方法

    编辑 删除 声明:ArrayList a, 仅仅只是声明了一个list变量,其未来作用相当于C++中的引用变量,亦或者相当于一个对象块的索引,但并未为其分配具体的完整的对象所需要的内存空间,其所分配的 ...

  3. 使用libsvm实现文本分类

    @Hcy(黄灿奕) 文本分类,首先它是分类问题,应该对应着分类过程的两个重要的步骤,一个是使用训练数据集训练分类器,另一个就是使用测试数据集来评价分类器的分类精度.然而,作为文本分类,它还具有文本这样 ...

  4. umask命令

    umask——显示.设置文件的缺省权限 the user file-creation mask 命令所在路径:Shell内置命令 示例1:显示缺省权限 # umask -S 参数S的作用是以rwx形式 ...

  5. 推荐一个高大上的网易云音乐命令行播放工具:musicbox

    网易云音乐上有很多适合程序猿的歌单,但是今天文章介绍的不是这些适合程序员工作时听的歌,而是一个用Python开发的开源播放器,专门适用于网易云音乐的播放.这个播放器的名称为MusicBox, 特色是用 ...

  6. (转)编写Spring的第一个案例并测试Spring的开发环境

    http://blog.csdn.net/yerenyuan_pku/article/details/52832145 Spring4.2.5的开发环境搭建好了之后,我们来编写Spring的第一个案例 ...

  7. windows快捷键cmd中

    windows 中cmd中命令: cls  ---------> 清屏 dir ----------> 获取目录 Ctrl + c ----> 结束当前命令 cd .. ------ ...

  8. TensorFlow中屏蔽warning的方法

    问题 使用sudo pip3 install tensorflow安装完CPU版tensorflow后,运行简单的测试程序,出现如下警告: I tensorflow/core/platform/cpu ...

  9. uva12099 The Bookcase

    这道题超经典.dp和优化都值得看一看.因为i+1只和i有关,用滚动数组节省空间暑假第一次做感觉很困难,现在看就清晰了很多 #include<cstdio> #include<cstr ...

  10. uva12174 滑动窗口+预处理

    注意理解题意,不是排列种类,而是下一个排序出现的时间滑动窗口,具体见代码,写了很多注释(紫书的思路1理解有点麻烦...)注:可以画一个轴来方便理解 #include<iostream> # ...