Codeforces 660C Hard Process【二分 Or 尺取】

时间:2022-09-03 22:17:53

题目链接:

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 - &lbrack;二分&plus;DP&rsqb;

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

  2. Educational Codeforces Round 53 &lpar;Rated for Div&period; 2&rpar; C&period; 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&period; Complete the Word(Codeforces Round &num;372 &lpar;Div&period; 2&rpar;) 尺取大法

    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&colon;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. 【转】require&period;js学习笔记&lpar;二&rpar;

    require.js遵循AMD规范,通过define定义模块,require异步加载模块,一个js文件即一个模块. 一.模块加载require1.加载符合AMD规范模块 HTML: <scrip ...

  2. Appcn 移动开发 前台与服务器数据交互

    第一次写.嘿嘿. 言归正传,这几天开始学习移动开发,使用的是Appcan平台.Appcan平台采用HTML5+CSS3做开发 实现跨平台,正好可以满足我们的业务需求. Appacn和数据库进行交互的方 ...

  3. 洞穴勘测(bzoj 2049)

    Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...

  4. lua&lowbar;cocos精灵的不断闪动

    一.   刚开始使用 local blink = cc.Blink:create(1, 10)   sprite:runAction(blink)                            ...

  5. OpenStack-Ocata版&plus;CentOS7&period;6 云平台环境搭建 — 3&period;安装配置OpenStack认证服务(keystone)

    节点配置信息说明: 控制节点:controller: IP:192.168.164.128 hostname&hosts:likeadmin 计算加点:Nova: IP:192.168.164 ...

  6. hdu 1005 根据递推公式构造矩阵 &lpar; 矩阵快速幂&rpar;

    f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Sample Input1 1 3 //a b n1 2 100 0 0 ...

  7. MySQL 8&period;0&period;11 报错&lbrack;ERROR&rsqb; &lbrack;MY-011087&rsqb; Different lower&lowbar;case&lowbar;table&lowbar;names settings for server &lpar;&&num;39&semi;1&&num;39&semi;&rpar;

    --报错信息: 2018-06-07T19:52:26.943083+08:00 0 [System] [MY-010116] [Server] /usr/local/mysql/bin/mysqld ...

  8. Vue&plus;webpack项目中实现跨域的http请求

    目前Vue项目中对json数据的请求一般使用两个插件vue-resource和axios, 但vue-resource已经不再维护, 而axios是官方推荐的且npm下载量已经170多万,github ...

  9. Ubuntu下安装LXC手记之centos

    宿主环境 Ubuntu 14.04 LTS 安装LXC $ sudo apt-get install lxc 安装完毕之后,运行lxc-checkconifg工具,检查当前Linux内核支持LXC的情 ...

  10. OC basic

    不能在栈上分配对象 //Interface type cannot be statically allocated NSString stackString; 因为对象所占内存总是分配在"堆 ...