660C - Hard Process - CodeForces Solution


binary search dp two pointers *1600

Please click on ads to support us..

C++ Code:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[300005];
int n,k;
int l,mid,r,ans;
int head,tail;//head和tail记录所需数列的首尾 
int mark[2];//mark[0]表示0在队列中出现的次数,mark[1]同理 
bool check(int x)
{
    memset(mark,0,sizeof(0));
    for(int i=1;i<=x;i++)
    {
        mark[a[i]]++;
    }
    if(mark[0]<=k)//如果一开始就满足的话直接退出 
    {
        head=1;tail=x;//那当然就是从第一个开始到第x个了 
        return 1;
    }
    for(int i=x+1;i<=n;i++)//i表示的是序列的尾巴的位置 
    {
        mark[a[i-x]]--;//去掉开头的那个数 
        mark[a[i]]++;//加上结尾的那个数 
        if(mark[0]<=k) 
        {
            head=i-x+1;tail=i;//尾巴是i,头就是i-x+1 
            return 1;
        }
    }
    return 0;
}
int main()
{
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
    l=0;r=n;//长度至少 为0(全是0还不准改),最多为n(全是1) 
    while(l<=r)//标准的二分板子 
    {
        mid=(r+l)>>1;
        if(check(mid))
        {
            ans=mid;
            l=mid+1;
        }
        else
        {
            r=mid-1;
        }
    }
    printf("%d\n",ans);//输出长度 
    for(int i=1;i<=n;i++)
    {
        if(i>=head&&i<=tail)//如果在队列中的话那肯定已经被我们改成1了 
        {
            printf("1 ");
        }
        else
        {
            printf("%d ",a[i]);//不在队列中的话就保持原状 
        }
    }
    return 0;
}
		   		 	  	  												  	


Comments

Submit
0 Comments
More Questions

1299A - Anu Has a Function
1111A - Superhero Transformation
954A - Diagonal Walking
39F - Pacifist frogs
1451C - String Equality
386A - Second-Price Auction
1690E - Price Maximization
282B - Painting Eggs
440A - Forgotten Episode
233B - Non-square Equation
628B - New Skateboard
262B - Roma and Changing Signs
755C - PolandBall and Forest
456B - Fedya and Maths
376B - IOU
1623B - Game on Ranges
1118A - Water Buying
1462C - Unique Number
301A - Yaroslav and Sequence
38A - Army
38C - Blinds
1197A - DIY Wooden Ladder
1717D - Madoka and The Corruption Scheme
1296D - Fight with Monsters
729D - Sea Battle
788A - Functions again
1245B - Restricted RPS
1490D - Permutation Transformation
1087B - Div Times Mod
1213B - Bad Prices