题目链接:

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

题意:

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

分析:

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

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

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

代码:

二分:

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
const int maxn = 300000 + 5;
int a[maxn];
int t[maxn];
int n, k;
int f1 = -1;
bool judge(int p)
{
for(int i = 0; i + p - 1< n; i++){
if(t[i + p - 1] - t[i - 1] + k >= p) {
f1 = i;
return true;
}
}
return false;
}
int main (void)
{
scanf("%d%d", &n, &k);
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
t[i] = t[i - 1] + (a[i] == 1);
}
int l = 0, r = n + 1;
while(l < r - 1){
int mid = (l + r) / 2;
if(judge(mid)) l = mid;
else r = mid;
}
printf("%d\n", l);
for(int i = 0; i < f1; i++){
printf("%d ", a[i]);
}
for(int i = f1; i < f1 + l; i++)
printf("1 ");
for(int i = max(f1 + l, 0); i < n; i++){
printf("%d ", a[i]);
}
return 0; }

尺取法:(two pointers)

#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;
const int maxn = 300000 + 5;
int a[maxn], t[maxn];
int n, k;
int main (void)
{
scanf("%d%d", &n, &k);
for(int i = 0; i < n; i++){
scanf("%d", &a[i]);
}
int r = 0, cnt = 1, ans = 0, MAX = 0;
int f1 = -1, f2 = -1;
for(int i = 0; i < n; i++){
if(a[i - 1] == 0){
cnt--;
while(cnt <= k && r < n){
if(a[r] == 1){
r++;
}else{
if(cnt == k) {break;}
cnt++;
r++;
}
}
ans = r - i ;
}else ans--;
if(ans > MAX){
MAX = ans;
f1 = i;
f2 = r - 1;
}
}
cout<<MAX<<endl;
if(k == 0){
for(int i = 0; i < n; i++) cout<<a[i]<<' ';
return 0;
}
for(int i = 0; i < f1; i++) cout<<a[i]<<' ';
for(int i = f1; i <= f2; i++) cout<<1<<' ';
for(int i = f2 + 1; i < n; i++) cout<<a[i]<<' ';
return 0;
}

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. 【HEVC帧间预测论文】P1.3 Fast Inter-Frame Prediction Algorithm of HEVC Based on Graphic Information

    基于图形信息的HEVC帧间预测快速算法/Fast Inter-Frame Prediction Algorithm of HEVC Based on Graphic Information <H ...

  2. Proteus与Keil连接及其仿真(有例子哦!)

    记录一下Proteus仿真的一些设置和使用,以方便自己以后复习和大家交流!如有错误,希望大家指正. 1.Proteus软件的安装,这里就不作说明了.

  3. 洛谷 P2680 运输计划

    题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家 ...

  4. 30行代码消费腾讯人工智能开放平台提供的自然语言处理API

    腾讯人工智能AI开放平台上提供了很多免费的人工智能API,开发人员只需要一个QQ号就可以登录进去使用. 腾讯人工智能AI开放平台的地址:https://ai.qq.com/ 里面的好东西很多,以自然语 ...

  5. dircolors - 设置‘ls'显示结果的颜色

    SYNOPSIS[总览] dircolors [-b] [--sh] [--bourne-shell] [-c] [--csh] [--c-shell] [-p] [--print-database] ...

  6. node.js中使用Redis

    服务端: 启动Redis服务: redis-server 客户端: 1.安装Redis        npm install redis --save 2.redisTest.js文件 //引入red ...

  7. python基础一 day3 列表方法

    ls=['a','b','c','d','a','b','c','d']lst=['e','f','g','h']# 增加# ls.append('a') 将元素a添加至列表ls的尾部# ls.ext ...

  8. IoC简介

    控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来降低程序代码之间的耦合度.其中最常见的方式叫做依赖注入(Dependency Injecti ...

  9. [BZOJ3207]:花神的嘲讽(分块解法)

    题目传送门 题目描述:背景花神是神,一大癖好就是嘲讽大J,举例如下:“哎你傻不傻的![hqz:大笨J]”“这道题又被J屎过了!!”“J这程序怎么跑这么快!J要逆袭了!”…… 描述这一天DJ在给吾等众蒟 ...

  10. Java中的类加载器--Class loader

    学习一下Java中的类加载器,这个是比较底层的东西,好好学习.理解一下.  一.类加载器的介绍 1.类加载器:就是加载类的工具,在java程序中用到一个类,java虚拟机首先要把这个类的字节码加载到内 ...