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任务个数 这个题 不是很 ...
随机推荐
- IOS Main函数
int main(int argc, char * argv[]) { @autoreleasepool { return UIApplicationMain(argc, argv, nil, NSS ...
- python学习之路-4 内置函数和装饰器
本篇涉及内容 内置函数 装饰器 内置函数 callable() 判断对象是否可以被调用,返回一个布尔值 1 2 3 4 5 6 7 8 9 10 11 num = 10 print(callabl ...
- hdoj 3400 三分
两次三分 #include <iostream> #include <cstdio> #include <cstring> #include <cmath&g ...
- web.cofing(新手必看)
花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...
- .net下载
1,Http 协议中有专门的指令来告知浏览器, 本次响应的是一个需要下载的文件. 格式如下: Content-Disposition: attachment;filename=filename.ext ...
- oracle导出数据显示出现ora-00109或者LRM-00109出错修改办法
出现这种问题是因为日期格式的问题,调整日期格式后就可以了 改成yyyy-mm-dd的格式就好了
- n个数的最大公约、最小公倍数
#include <cstdio> #include <cstring> using namespace std; #define N 1010 //两个数的最大公约数和最小公 ...
- iOS UIWebView 访问https 绕过证书验证的方法
在文件开始实现 allowsAnyHTTPSCertificateForHost 方法 @implementation NSURLRequest (NSURLRequestWithIgnoreSSL ...
- 远程复制 scp命令
定义 本机为A,用户名为usera,登录远程主机B的为userb,IP为remote_ip 1. 从B 拷贝文件到A机器 用下面的命令 scp userb@remote_ip:remote_path ...
- 九、 合成(Composite)模式 --结构模式(Structural Pattern)
合成模式:有时又叫做部分-整体模式(Part-Whole).合成模式将对象组织到树结构中,可以用来描述整体与部分的关系.合成模式可以使客户端将单纯元素与复合元素同等看待. 合成模式分为安全式和透明式 ...