Hard Process(二分)

Hard Process

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status Practice CodeForces 660C

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 = 300010;
int num[MAXN];
int a[MAXN];
int L, n, k;

int js(int x){
    for(int i = 0; i + x <= n; i++){
        if(num[i + x] - num[i] <= k){
            L = i + 1;
            return true;
        }
    }
    return false;
}
int erfen(int l, int r){
    int mid, ans;
    while(l <= r){
        mid = (l + r) >> 1;
        if(js(mid)){
            ans = mid;
            l = mid + 1;
        }
        else
            r = mid - 1;
    }
    return ans;
}
int main(){
    while(~scanf("%d%d",&n, &k)){
        int temp;
        memset(num, 0, sizeof(num));
        for(int i = 1; i <= n; i++){
            scanf("%d", a + i);
            num[i] = num[i - 1] + (a[i] == 0);
        }
        int ans = erfen(0,n);
        for(int i = L; i < L + ans; i++){
            a[i] = 1;
        }
        printf("%d\n", ans);
        for(int i = 1; i <= n; i++){
            if(i != 1)printf(" ");
            printf("%d",a[i]);
        }puts("");
    }
    return 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 = 1000100;
int num[MAXN];
int pos[MAXN];
int p[MAXN];
int main(){
    int n, k;
    while(~scanf("%d%d",&n, &k)){
        int gg = 0;
        for(int i = 0; i < n; i++){
            scanf("%d", num + i);
            if(num[i] == 1)gg = 1;
        }
        if(!gg){
            printf("%d\n",k);
            for(int i = 0; i < k; i++){
                if(i)printf(" ");
                printf("1");
            }
            for(int i = k; i < n; i++){
                if(i)printf(" ");
                printf("0");
            }puts("");
            continue;
        }
        int kg = 0, ans = 0, temp = 0, cnt = 0, tp = 0;
        for(int i = 0; i < n; i++){
            if(kg == 0 && num[i] == 1){
                    temp = 0;
                    kg = 1;
                    cnt = 0;
                    for(int j = i; j < n; j++){

                        if(num[j] == 1){
                            temp++;
                        }
                        else{
                            if(cnt + 1 > k)break;
                            pos[cnt++] = j;
                            temp++;
                        }
                    }
                        int j = i;
                        while(cnt < k && j > 0){
                            pos[cnt++] = --j;
                            temp++;
                        }

                    if(ans < temp){
                        ans = temp;
                        tp = cnt;
                        for(int j = 0; j < tp; j++){
                            p[j] = pos[j];
                        }
                    }
                }
            if(num[i] == 0)kg = 0;
        }
        for(int i = 0; i < tp; i++){
        //    printf("%d ",p[i]);
            num[p[i]] = 1;
        }//puts("");
        printf("%d\n", ans);
        for(int i = 0; i < n; i++){
            if(i)printf(" ");
            printf("%d",num[i]);
        }
        puts("");
    }
    return 0;
}
时间: 2024-10-10 13:59:32

Hard Process(二分)的相关文章

二分+DP HDU 3433 A Task Process

HDU 3433 A Task Process Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1368    Accepted Submission(s): 684 Problem Description There are two kinds of tasks, namely A and B. There are N workers

HDU 3433 (DP + 二分) A Task Process

题意: 有n个员工,每个员工完成一件A任务和一件B任务的时间给出,问要完成x件A任务y件B任务所需的最短时间是多少 思路: DP + 二分我也是第一次见到,这个我只能说太难想了,根本想不到. dp[i][j]表示在t时间内前i个人完成j件A任务后所能完成B任务的最大数量. 代码中还有一些注释. 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 using

HDU5008 Boring String Problem(后缀数组 + 二分 + 线段树)

题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5008 Description In this problem, you are given a string s and q queries. For each query, you should answer that when all distinct substrings of string s were sorted lexicographically, which one is

hdu-1025 Constructing Roads In JGShining&#39;s Kingdom(二分查找)

题目链接: Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21045    Accepted Submission(s): 5950 Problem Description JGShining's kingdom consists of 2n(n is

Gunner II(二分,map,数字转化)

Gunner II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1740    Accepted Submission(s): 635 Problem Description Long long ago, there was a gunner whose name is Jack. He likes to go hunting ve

POJ 2112 最大流+二分+(建图:最重要的)

Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 12521   Accepted: 4524 Case Time Limit: 1000MS Description FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 <= C <= 200) co

hdu 5008(2014 ACM/ICPC Asia Regional Xi&#39;an Online ) Boring String Problem(后缀数组&amp;二分)

Boring String Problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 219    Accepted Submission(s): 45 Problem Description In this problem, you are given a string s and q queries. For each que

hdu 4893 (多校1007)Wow! Such Sequence!(线段树&amp;二分&amp;思维)

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 352    Accepted Submission(s): 104 Problem Description Recently, Doge got a funny birthday present from his new friend, Prot

POJ 3484 Showstopper 二分

 Showstopper Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1218   Accepted: 356 Description Data-mining huge data sets can be a painful and long lasting process if we are not aware of tiny patterns existing within those data sets. On