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 ...
随机推荐
- 【程小白】Java基本特性
一.StringBuffer.StringBuilder的区别 StringBuffer是线程安全的,StringBuilder是线程不安全的.所以以后在单线程中,如果涉及大量字符串操作,还是用Str ...
- java将一个List赋值给另一个List的4种方法
编辑 删除 声明:ArrayList a, 仅仅只是声明了一个list变量,其未来作用相当于C++中的引用变量,亦或者相当于一个对象块的索引,但并未为其分配具体的完整的对象所需要的内存空间,其所分配的 ...
- 使用libsvm实现文本分类
@Hcy(黄灿奕) 文本分类,首先它是分类问题,应该对应着分类过程的两个重要的步骤,一个是使用训练数据集训练分类器,另一个就是使用测试数据集来评价分类器的分类精度.然而,作为文本分类,它还具有文本这样 ...
- umask命令
umask——显示.设置文件的缺省权限 the user file-creation mask 命令所在路径:Shell内置命令 示例1:显示缺省权限 # umask -S 参数S的作用是以rwx形式 ...
- 推荐一个高大上的网易云音乐命令行播放工具:musicbox
网易云音乐上有很多适合程序猿的歌单,但是今天文章介绍的不是这些适合程序员工作时听的歌,而是一个用Python开发的开源播放器,专门适用于网易云音乐的播放.这个播放器的名称为MusicBox, 特色是用 ...
- (转)编写Spring的第一个案例并测试Spring的开发环境
http://blog.csdn.net/yerenyuan_pku/article/details/52832145 Spring4.2.5的开发环境搭建好了之后,我们来编写Spring的第一个案例 ...
- windows快捷键cmd中
windows 中cmd中命令: cls ---------> 清屏 dir ----------> 获取目录 Ctrl + c ----> 结束当前命令 cd .. ------ ...
- TensorFlow中屏蔽warning的方法
问题 使用sudo pip3 install tensorflow安装完CPU版tensorflow后,运行简单的测试程序,出现如下警告: I tensorflow/core/platform/cpu ...
- uva12099 The Bookcase
这道题超经典.dp和优化都值得看一看.因为i+1只和i有关,用滚动数组节省空间暑假第一次做感觉很困难,现在看就清晰了很多 #include<cstdio> #include<cstr ...
- uva12174 滑动窗口+预处理
注意理解题意,不是排列种类,而是下一个排序出现的时间滑动窗口,具体见代码,写了很多注释(紫书的思路1理解有点麻烦...)注:可以画一个轴来方便理解 #include<iostream> # ...