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

Input
7 1 1 0 0 1 1 0 1
Output
4 1 0 0 1 1 1 1
Input
10 2 1 0 0 1 0 1 0 1 0 1
Output
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(二分)的更多相关文章

  1. hdu 3433 A Task Process 二分+dp

    A Task Process Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  2. Educational Codeforces Round 11 C. Hard Process 二分

    C. Hard Process 题目连接: http://www.codeforces.com/contest/660/problem/C Description You are given an a ...

  3. codeforces 660C C. Hard Process(二分)

    题目链接: C. Hard Process time limit per test 1 second memory limit per test 256 megabytes input standar ...

  4. Codeforces 660C - Hard Process - [二分+DP]

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

  5. hdu3433A Task Process( 二分dp)

    链接 二分时间,在时间内dp[i][j]表示截止到第i个人已经做了j个A最多还能做多少个B #include <iostream> #include<cstdio> #incl ...

  6. 二分+DP HDU 3433 A Task Process

    HDU 3433 A Task Process Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  7. HDU 3433 (DP + 二分) A Task Process

    题意: 有n个员工,每个员工完成一件A任务和一件B任务的时间给出,问要完成x件A任务y件B任务所需的最短时间是多少 思路: DP + 二分我也是第一次见到,这个我只能说太难想了,根本想不到. dp[i ...

  8. Educational Codeforces Round 11 C. Hard Process 前缀和+二分

    题目链接: http://codeforces.com/contest/660/problem/C 题意: 将最多k个0变成1,使得连续的1的个数最大 题解: 二分连续的1的个数x.用前缀和判断区间[ ...

  9. hdu 3433 A Task Process(dp+二分)

    题目链接 题意:n个人, 要完成a个x任务, b个y任务. 求,最短的时间 思路:由于时间较大,用 二分来找时间. dp[i][j]表示 i个人完成j个x任务, 最多能完成的y任务个数 这个题 不是很 ...

随机推荐

  1. 删除list中指定值的元素

    public class ListRemoveAll { public static void main(String[] args) {  // TODO Auto-generated method ...

  2. Oracle与DB2的区别

    系统结构概述 首先,我们需要理解 Oracle 使用的架构,并理解它与 DB2 的不同之处.图 1 展示了 Oracle 的系统结构.将该图与 图 2 进行比较,后者显示了 DB2 的系统结构.在阅读 ...

  3. Eclipse 里的 Classpath Variables M2_REPO 无法修改(maven)

      解决方法: 在C:\Documents and Settings\Administrator\.m2中放入setting.xml,并修改本地仓库为 <localRepository>D ...

  4. Android应用程序框架层和系统运行库层日志系统源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6598703 在开发Android应用程序时,少 ...

  5. kaggle之泰坦尼克的沉没

    Titanic 沉没 参见:https://github.com/lijingpeng/kaggle 这是一个分类任务,特征包含离散特征和连续特征,数据如下:Kaggle地址.目标是根据数据特征预测一 ...

  6. 微信网页授权获取code链接

    本公众号授权 "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&r ...

  7. stl学习之字符串

    其实string也是stl里面的内容,记录几点自己不常用的内容 1.at方法(比[]会判断是否越位) 2. int copy(char *s, int n, int pos=0) const; 把当前 ...

  8. (原)opencv中使用限制对比度自适应直方图均衡CLAHE

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5462656.html Ptr<CLAHE> clahe = createCLAHE(); ...

  9. shell中的退出状态码

    shell中的退出状态码最大只有255,如果超过这个值,就会进行取余运算,即如果执行如下命令: exit exitCode 如果exitCode大于255,那么实际的状态码为exitCode % 25 ...

  10. PHP设置http头信息

    <?PHP function https($num) { $http = array ( 100 => "HTTP/1.1 100 Continue", 101 =&g ...