Hard Process(二分)
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
You are given an array a with n elements. Each element of a is either 0 or 1.
Let's denote the length of the longest subsegment of consecutive elements in a, consisting of only numbers one, as f(a). You can change no more than k zeroes to ones to maximize f(a).
Input
The first line contains two integers n and k (1 ≤ n ≤ 3·105, 0 ≤ k ≤ n) — the number of elements in a and the parameter k.
The second line contains n integers ai (0 ≤ ai ≤ 1) — the elements of a.
Output
On the first line print a non-negative integer z — the maximal value of f(a) after no more than k changes of zeroes to ones.
On the second line print n integers aj — the elements of the array a after the changes.
If there are multiple answers, you can print any one of them.
Sample Input
7 1 1 0 0 1 1 0 1
4 1 0 0 1 1 1 1
10 2 1 0 0 1 0 1 0 1 0 1
5 1 0 0 1 1 1 1 1 0 1
题解:让改变k个数,使序列连续1的个数最大,我们可以求0的前缀和,然后通过二分来找位置;我竟然用暴力超时了好长时间。。。
二分:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN = ;
int num[MAXN];
int a[MAXN];
int L, n, k; int js(int x){
for(int i = ; i + x <= n; i++){
if(num[i + x] - num[i] <= k){
L = i + ;
return true;
}
}
return false;
}
int erfen(int l, int r){
int mid, ans;
while(l <= r){
mid = (l + r) >> ;
if(js(mid)){
ans = mid;
l = mid + ;
}
else
r = mid - ;
}
return ans;
}
int main(){
while(~scanf("%d%d",&n, &k)){
int temp;
memset(num, , sizeof(num));
for(int i = ; i <= n; i++){
scanf("%d", a + i);
num[i] = num[i - ] + (a[i] == );
}
int ans = erfen(,n);
for(int i = L; i < L + ans; i++){
a[i] = ;
}
printf("%d\n", ans);
for(int i = ; i <= n; i++){
if(i != )printf(" ");
printf("%d",a[i]);
}puts("");
}
return ;
}
暴力超时:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
const int MAXN = ;
int num[MAXN];
int pos[MAXN];
int p[MAXN];
int main(){
int n, k;
while(~scanf("%d%d",&n, &k)){
int gg = ;
for(int i = ; i < n; i++){
scanf("%d", num + i);
if(num[i] == )gg = ;
}
if(!gg){
printf("%d\n",k);
for(int i = ; i < k; i++){
if(i)printf(" ");
printf("");
}
for(int i = k; i < n; i++){
if(i)printf(" ");
printf("");
}puts("");
continue;
}
int kg = , ans = , temp = , cnt = , tp = ;
for(int i = ; i < n; i++){
if(kg == && num[i] == ){
temp = ;
kg = ;
cnt = ;
for(int j = i; j < n; j++){ if(num[j] == ){
temp++;
}
else{
if(cnt + > k)break;
pos[cnt++] = j;
temp++;
}
}
int j = i;
while(cnt < k && j > ){
pos[cnt++] = --j;
temp++;
} if(ans < temp){
ans = temp;
tp = cnt;
for(int j = ; j < tp; j++){
p[j] = pos[j];
}
}
}
if(num[i] == )kg = ;
}
for(int i = ; i < tp; i++){
// printf("%d ",p[i]);
num[p[i]] = ;
}//puts("");
printf("%d\n", ans);
for(int i = ; i < n; i++){
if(i)printf(" ");
printf("%d",num[i]);
}
puts("");
}
return ;
}
Hard Process(二分)的更多相关文章
- hdu 3433 A Task Process 二分+dp
A Task Process Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process 题目连接: http://www.codeforces.com/contest/660/problem/C Description You are given an a ...
- codeforces 660C C. Hard Process(二分)
题目链接: C. Hard Process time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Codeforces 660C - Hard Process - [二分+DP]
题目链接:http://codeforces.com/problemset/problem/660/C 题意: 给你一个长度为 $n$ 的 $01$ 串 $a$,记 $f(a)$ 表示其中最长的一段连 ...
- hdu3433A Task Process( 二分dp)
链接 二分时间,在时间内dp[i][j]表示截止到第i个人已经做了j个A最多还能做多少个B #include <iostream> #include<cstdio> #incl ...
- 二分+DP HDU 3433 A Task Process
HDU 3433 A Task Process Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- HDU 3433 (DP + 二分) A Task Process
题意: 有n个员工,每个员工完成一件A任务和一件B任务的时间给出,问要完成x件A任务y件B任务所需的最短时间是多少 思路: DP + 二分我也是第一次见到,这个我只能说太难想了,根本想不到. dp[i ...
- Educational Codeforces Round 11 C. Hard Process 前缀和+二分
题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...
- hdu 3433 A Task Process(dp+二分)
题目链接 题意:n个人, 要完成a个x任务, b个y任务. 求,最短的时间 思路:由于时间较大,用 二分来找时间. dp[i][j]表示 i个人完成j个x任务, 最多能完成的y任务个数 这个题 不是很 ...
随机推荐
- 修改UISearchBar placeholder textColor
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
- [Ext JS 4] 实战之Grid, Tree Gird编辑Cell
前言 本篇这里以稍微复杂一点的Tree Grid 来介绍. 在写编辑grid 之, 先来看一下 grid 的 selType 的配置. 先给一个简单的Tree grid 的例子: Ext.onRead ...
- [置顶] Android项目组织和代码重用
在Android应用开发过程中,只要涉及两个或以上人的开发,就需要考虑分工和代码的组织和重用问题. 代码重用有三种方式: 1.APK: 2.JAR:通过Libs/ 和Build path集成,缺点是不 ...
- boost::asio译文
Christopher Kohlhoff Copyright © 2003-2012 Christopher M. Kohlhoff 以Boost1.0的软件授权进行发布(见附带的LICENS ...
- Unity 启动画面淡入淡出
前几天在玩我叫MT 2游戏的时候发现打开他们应用的时候发现他们Logo淡入淡出的效果做的挺好的,例如第一张是运营商腾讯的logo第二张是他们公司的游戏logo.我们也来模仿一下: 第一张图片:运营商的 ...
- 12款 jquery轮播插件
Nivo Slider 首先推荐的这款插件号称世界上最棒的图片轮播插件,有独立的 jQuery 插件和 WordPress 插件两个版本. 目前下载量已经突破 1,800,000 次!jQuery 独 ...
- vue中关于computed的一点理解
computed相当于属性的一个实时计算,如果实时计算里关联了对象,那么当对象的某个值改变的时候,同事会出发实时计算. 例子: <body id="content"> ...
- javascript 获取event对象
//转载处 http://www.cnblogs.com/funlake/archive/2009/04/07/1431238.html 非常详细 先从一个简单的例子说起,一个简单的button控件如 ...
- JS正则表达式收集篇
1.验证只可输入整数或小数点后两位的数字:/^([1-9]{1}|[1-9]{1}[0-9])+(.[1-9]{1,2})?$/ 2.验证Email: /^([a-zA-Z0-9]+[_|\_|\.] ...
- TransactionScrope
测这个东西其实是由生产环境数据库报错,ORA-14450错误. 测试结果是: 1)使用transactionscrope时,数据库连接打开需在scrope内打开: 2)TransactionScope ...