codeforces_660C. Hard Process(尺取法)

C. Hard Process
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Examples
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
又是一道尺取法,不过我想复杂了些,枚举了每一个0,这样后面就必须有全0,无0特判,再加上一个k==0特判。后来发现还能用二分做,而且直观了些。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#define Si(a) scanf("%d",&a)
#define Sl(a) scanf("%lld",&a)
#define Sd(a) scanf("%lf",&a)
#define Ss(a) scanf("%s",a)
#define Pi(a) printf("%d\n",(a))
#define Pl(a) printf("%lld\n",(a))
#define Pd(a) printf("%lf\n",(a))
#define Ps(a) printf("%s\n",(a))
#define W(a) while(a--)
#define mem(a,b) memset(a,(b),sizeof(a))
#define FOP freopen("data.txt","r",stdin)
#define inf 0x3f3f3f3f
#define maxn 1000010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

int a[300010];
struct One
{
    int i;
    int num;
} one[300010];
vector<int> zero;
int main()
{
    //FOP;
    int n,k,i=0,j=0;
    Si(n);
    Si(k);
    int flag=0;
    int numm=0,top=0;
    for(i=1; i<=n; i++)
    {
        Si(a[i]);
        if(a[i]==0)
        {
            zero.push_back(i);
            numm=0;
        }
        else if(a[i]==1)
        {
            one[i].i=i;
            numm++;
            one[i].num=numm;
        }
    }
    int ansi=1,ansj=1,num=0,maxx=0,nk=0;
    int len=zero.size();
    for(i=0,j=0; j<len; j++)
    {
        if(a[zero[j]-1]==1)num+=one[zero[j]-1].num;
        if(nk==k)
        {
            if(maxx<num)
            {
                maxx=num;
                ansi=zero[i];
                ansj=zero[j-1];
            }
            nk--;
            num--;
            if(a[zero[i]-1]==1)num-=one[zero[i]-1].num;
            i++;
        }
        nk++;
        num++;
    }
    if(a[n]==1)
    {
        if(nk==k)
        {
            if(a[ansi-1]==1)num-=one[ansi-1].num;
        }
        num+=one[n].num;
    }
    if(num>maxx&&len!=0)
    {
        maxx=num;
        ansi=zero[i];
        ansj=n;
    }

    if(k==0)
    {
        ansi=0;ansj=0;
        for(i=1; i<=n; i++)
            if(maxx<one[i].num)maxx=one[i].num;
    }
    else if(zero.size()==0)
        maxx=num;
    else if(zero.size()==n)
    {
        maxx=k;
        ansi=1;
        ansj=k;
    }
    for(i=ansi; i<=ansj; i++)a[i]=1;
    Pi(maxx);
    for(i=1; i<=n; i++)
    {
        printf("%d",a[i]);
        if(i!=n)printf(" ");
    }
    printf("\n");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值