Hard Process(二分)

时间:2022-09-03 22:17:23
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 二分&plus;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&period; Hard Process 二分

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

  3. codeforces 660C C&period; Hard Process&lpar;二分&rpar;

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

  4. Codeforces 660C - Hard Process - &lbrack;二分&plus;DP&rsqb;

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

  5. hdu3433A Task Process&lpar; 二分dp)

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

  6. 二分&plus;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 &lpar;DP &plus; 二分&rpar; A Task Process

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

  8. Educational Codeforces Round 11 C&period; Hard Process 前缀和&plus;二分

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

  9. hdu 3433 A Task Process&lpar;dp&plus;二分&rpar;

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

随机推荐

  1. HTTP 权威指南

    第一章 http概述 1.3.1 媒体类型 - http为每种web传输的数据格式加上MIME类型数据标签(multipurpose internet mail 1.4 事务 一个请求 + 一个响应 ...

  2. static、final、static final 用法

    1.使用范围:类.方法.变量.2.区别和联系:2.1.static 含义:静态的,被 static 修饰的方法和属性只属于类不属于类的任何对象.2.2.static 用法:2.2.1.static 可 ...

  3. Solution multisite htaccess cleanURL

    My solution to getting Clean URL working with my multisite setup drupal 4.7 I added Alias to my http ...

  4. golang语言部分保留字的举例

    golang和c的代码有很大不同的,一不小心就会误用. 1 /* go保留字: */ /* break default func interface select case defer go map ...

  5. Mysql数据库防SQL注入原理

    每个语言都有自己的数据库框架或库,无论是哪种语言,哪种库,它们在数据库防注入方面使用的技术原理无外乎下面介绍的几种方法. 一.特殊字符转义处理 Mysql特殊字符指在mysql中具有特殊含义的字符,除 ...

  6. 解决div里面img标签后面跟着空白符

    直接在div里面添加img标签有一个坑,img的高度会莫名添加几个像素的高度,查询之后才知道原来css2出现的问题. 引起这个问题原来是img标签默认基线引起的,vertical-align:base ...

  7. dtIntersectSegmentPoly2D 2D上的线段与多边形相交计算 产生结果:是否相交,线段跨越的开始和结束百分比,相交的边

    dtIntersectSegmentPoly2D(startPos, endPos, verts, nv, tmin, tmax, segMin, segMax): http://geomalgori ...

  8. &lbrack;JS深入学习&rsqb;——数组对象排序

    (转) JavaScript实现多维数组.对象数组排序,其实用的就是原生的sort()方法,用于对数组的元素进行排序. sort() 方法用于对数组的元素进行排序.语法如下: arrayObject. ...

  9. Python使用logging来记录日志

    #encoding:utf-8 import logging.config from logging.handlers import RotatingFileHandler import Config ...

  10. shell脚本报错 value too great for base

    此错误是shell脚本在计算以0开头的数字时,默认以8进制进行计算,导致在计算08时超过了8进制的范围,报此错误. shell脚本代码如下: #!/bin/bash a= ..} do a=$[$a+ ...