Codeforces 660C Hard Process【二分 Or 尺取】
题目链接:
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 尺取】的更多相关文章
- Codeforces 660C - Hard Process - [二分+DP]
题目链接:http://codeforces.com/problemset/problem/660/C 题意: 给你一个长度为 $n$ 的 $01$ 串 $a$,记 $f(a)$ 表示其中最长的一段连 ...
- 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, ...
- POJ-3061 Subsequence 二分或尺取
题面 题意:给你一个长度为n(n<100000)的数组,让你找到一个最短的连续子序列,使得子序列的和>=m (m<1e9) 题解: 1 显然我们我们可以二分答案,然后利用前缀和判断 ...
- 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 ...
- Codeforces - 1191E - Tokitsukaze and Duel - 博弈论 - 尺取
https://codeforc.es/contest/1191/problem/E 参考自:http://www.mamicode.com/info-detail-2726030.html 和官方题 ...
- Codeforces 660C Hard Process(尺取法)
题目大概说给一个由01组成的序列,要求最多把k个0改成1使得连续的1的个数最多,输出一种方案. 和CF 676C相似. #include<cstdio> #include<algor ...
- POJ:3579-Median(二分+尺取寻找中位数)
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9201 Accepted: 3209 Description Gi ...
- poj 3579 Median 二分套二分 或 二分加尺取
Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5118 Accepted: 1641 Descriptio ...
- codeforces 660C Hard Process
维护一个左右区间指针就可以. #include<cstdio> #include<cstring> #include<iostream> #include<q ...
随机推荐
- Android CursorAdapter的使用
CursorAdapter继承于BaseAdapter,为Cursor和ListView连接提供了桥梁. 首先看一下CursorAdapter的部分源码: /** * @see android.wid ...
- vb6如何调用delphi DLL中的函数并返回字符串?
1,问题描述 最近发现vb6调用delphi DLL中的函数并返回字符串时出现问题,有时正常,有时出现?号,有时干脆导致VB程序退出 -- :: 将金额数字转化为可读的语音文字:1转化为1元 ???? ...
- java中properties的使用实例
package com.ywx.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputSt ...
- IOS问题
#import "EXFifthViewController.h" @interface EXFifthViewController () @end @implementation ...
- gym 100947I (求因子)
What a Mess Alex is a very clever boy, after all he is the son of the greatest watchmaker in Odin. O ...
- C语言常用关键字及运算符操作---关键字
每个知识点4问: 1. 是什么? 2. 什么时间用? 3. 怎么用? 4.为什么这么用? 1. 32个关键字 //(1)sizeof 的用法 //sizeof 是关键字,让编译器帮我们查看内存空间存储 ...
- 油猴和EX-百度脚本 百度网盘下载
pansoso.com 搜网盘 油猴和EX-百度脚本.zip https://aleikeji.pipipan.com/fs/845023-331102839
- Perl 安装 JSON 包
$tar xvfz JSON.tar.gz $cd JSON $perl Makefile.PL $make $make install
- OpenCV2:第十章 视频操作
一.简介 OpenCV提供了专门操作视频的接口类VideoCapture 二.构造VideoCapture类 VideoCapture::VideoCapture() VideoCapture::Vi ...
- CF1179D Fedor Runs for President [DP,斜率优化]
Codeforces 思路 考虑把连的那两个点中间的链提出来,那么就会变成一条链,链上的每个点挂着一棵子树的形式. 设那些子树的大小为\(S_1,S2,\cdots\),那么新加的简单路径个数就是 \ ...