[Codeforces 883D]Packmen Strike Back

883D

题解:
特殊处理只有一个 P “ P ” 的情况。
对于有两个以上的 P “ P ” ,第一问的答案一定是所有 “ ∗ ” 的个数。
显然第二问的答案具有单调性,所以二分答案,对于一个长度 len l e n ,只要从左到右分配方向分几种情况贪心处理就行了。
贪心策略:
1. 如果当前的 P “ P ” 的左边没有没被覆盖的 “ ∗ ” ,那么方向一定是向右。
2. 如果当前的 P “ P ” 向左并不能覆盖当前没被覆盖的最左边的 “ ∗ ” ,则长度len是不合法的。
3. 如果当前的 P “ P ” 左边的没有其他的 P “ P ” ,或者如果当前的 P “ P ” 向左不能代替前一个 P “ P ” ,则直接把这个 P “ P ” 设定为向左,不改变前面 P “ P ” 的朝向。
4. 如果当前的 P “ P ” 向左能够代替前一个 P “ P ” ,则将前一个 P “ P ” 的方向设置成向右。
5. 如果当前的 P “ P ” 前面一个 P “ P ” 之前执行过 4 4 , 且,当前的P能够替代前一个 P “ P ” ,则将当前的 P “ P ” 朝向设为向左,前一个 P “ P ” 朝向设为向右,前面第二个 P “ P ” 的朝向设为向左。

时间复杂度: O(nlogn) O ( n l o g n )

代码:

#include<bits/stdc++.h>
#define LL long long
#define ull unsigned long long
#define ULL ull
#define mp make_pair
#define pii pair<int,int>
#define piii pair<int, pii >
#define pll pair <ll,ll>
#define pb push_back
#define big 20160116
#define INF 2147483647
#define pq priority_queue
#define rank rk124232
#define y1 y20160116
#define y0 y20160110
using namespace std;
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
namespace Mymath{
    LL qp(LL x,LL p,LL mod){
        LL ans=1;
        while (p){
            if (p&1) ans=ans*x%mod;
            x=x*x%mod;
            p>>=1;
        }
        return ans;
    }
    LL inv(LL x,LL mod){
        return qp(x,mod-2,mod);
    }
    LL C(LL N,LL K,LL fact[],LL mod){
        return fact[N]*inv(fact[K],mod)%mod*inv(fact[N-K],mod)%mod;
    }
    template <typename Tp> Tp gcd(Tp A,Tp B){
        if (B==0) return A;
        return gcd(B,A%B);
    }
    template <typename Tp> Tp lcm(Tp A,Tp B){
        return A*B/gcd(A,B);
    }
};
namespace fwt{
    using namespace Mymath;
    void FWT(int a[],int n,LL mod)
    {
        for(int d=1;d<n;d<<=1)
            for(int m=d<<1,i=0;i<n;i+=m)
                for(int j=0;j<d;j++)
                {
                    int x=a[i+j],y=a[i+j+d];
                    a[i+j]=(x+y)%mod,a[i+j+d]=(x-y+mod)%mod;
                    //xor:a[i+j]=x+y,a[i+j+d]=x-y;
                    //and:a[i+j]=x+y;
                    //or:a[i+j+d]=x+y;
                }
    }

    void UFWT(int a[],int n,LL mod)
    {
        LL rev=inv(2,mod);
        for(int d=1;d<n;d<<=1)
            for(int m=d<<1,i=0;i<n;i+=m)
                for(int j=0;j<d;j++)
                {
                    int x=a[i+j],y=a[i+j+d];
                    a[i+j]=1LL*(x+y)*rev%mod,a[i+j+d]=(1LL*(x-y)*rev%mod+mod)%mod;
                    //xor:a[i+j]=(x+y)/2,a[i+j+d]=(x-y)/2;
                    //and:a[i+j]=x-y;
                    //or:a[i+j+d]=y-x;
                }
    }
    void solve(int a[],int b[],int n,LL mod)
    {
        FWT(a,n,mod);
        FWT(b,n,mod);
        for(int i=0;i<n;i++) a[i]=1LL*a[i]*b[i]%mod;
        UFWT(a,n,mod);
    }
};
const int Maxn=1e6+5;
int n;
char c[Maxn];
int ppos[Maxn],kp;
int apos[Maxn],ka;
int dir[Maxn];// 1:right -1:left 2: right(const) -2:left(have ancestor)
int fc[Maxn];
int fc2[Maxn];
bool check(int l){
    ka=kp=0;
    for (int i=0;i<n;i++){
        if (c[i]=='P'){
            ppos[kp++]=i;
        }
    }
    for (int i=0;i<n;i++){
        if (c[i]=='*'){
            apos[ka++]=i;
        }
    } 
    int nowgt=0;
    for (int i=0;i<kp;i++){
        //cout<<i<<' '<<nowgt<<endl;
        if (ppos[i]<apos[nowgt]){
            dir[i]=2;
            while (nowgt<ka && ppos[i]+l>=apos[nowgt]) nowgt++;
            if (nowgt==ka) return true;
        }
        else{
            if (ppos[i]-l>apos[nowgt]) return false;
            if (i==0){
                dir[i]=-1;
                fc[i]=nowgt;
                while (nowgt<ka && ppos[i]>=apos[nowgt]) nowgt++;
                if (nowgt==ka) return true;
                continue;
            }
            if (dir[i-1]==-1){
                int xp=apos[fc[i-1]];
                if (ppos[i]-l<=xp){
                    dir[i-1]=1;
                    dir[i]=-2;
                    fc2[i]=nowgt;
                    while (nowgt<ka && ppos[i-1]+l>=apos[nowgt]) nowgt++;
                    if (nowgt==ka) return true;
                    fc[i]=fc[i-1];
                }
                else{
                    fc[i]=nowgt;
                    dir[i]=-1;
                    while (nowgt<ka && ppos[i]>=apos[nowgt]) nowgt++;
                    if (nowgt==ka) return true;
                }
            }
            else if (dir[i-1]==-2){
                int xp=apos[fc[i-1]];
                if (ppos[i]-l<=xp){
                    dir[i-1]=1;
                    dir[i]=-2;
                    fc2[i]=nowgt;
                    while (nowgt<ka && ppos[i-1]+l>=apos[nowgt]) nowgt++;
                    if (nowgt==ka) return true;
                    fc[i]=fc[i-1];
                }
                else{
                    xp=apos[fc2[i-1]];
                    if (ppos[i]-l<=xp){
                        dir[i-2]=-1;
                        dir[i-1]=1;
                        dir[i]=-2;
                        fc2[i]=nowgt;
                        fc[i]=fc[i-1];
                        while (nowgt<ka && ppos[i-1]+l>=apos[nowgt]) nowgt++;
                        if (nowgt==ka) return true;
                    }
                    else{
                        fc[i]=nowgt;
                        dir[i]=-1;
                        while (nowgt<ka && ppos[i]>=apos[nowgt]) nowgt++;
                        if (nowgt==ka) return true;
                    }
                }
            }
            else{
                fc[i]=nowgt;
                dir[i]=-1;
                while (nowgt<ka && ppos[i]>=apos[nowgt]) nowgt++;
                if (nowgt==ka) return true;
            }
        }
    }
    return false;
}
int main(){
    scanf("%d",&n);
    scanf("%s",c);
    int cnt=0;
    int pos;
    int ca=0;
    for (int i=0;i<n;i++){
        if (c[i]=='P') cnt++,pos=i;
        if (c[i]=='*') ca++;
    }
    if (cnt==1){
        int c1=0,c2=0;
        int l=n,r=-1;
        for (int i=0;i<pos;i++){
            if (c[i]=='*') c1++,l=min(l,i);
        }
        for (int i=pos+1;i<n;i++){
            if (c[i]=='*') c2++,r=max(r,i);
        }
        if(c1>c2){
            printf("%d %d\n",c1,pos-l);
        }
        else if (c2>c1){
            printf("%d %d\n",c2,r-pos);
        }
        else{
            printf("%d %d\n",c1,min(pos-l,r-pos));
        }
        return 0;
    }
    //cerr<<123<<endl;
    printf("%d ",ca);
    int lo=0,hi=n;
    while (hi-lo>1){
        int mid=lo+hi>>1;
        if (check(mid)){
            hi=mid;
        }
        else{
            lo=mid;
        }
    }
    printf("%d\n",hi);
}
/*
6
*.P*P*
*/ 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值