20分求调,悬赏一关!

P1439 【模板】最长公共子序列

awaken1802 @ 2023-05-07 19:49:55

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
long long n,ans,a1[100010],a2[100010],dp1[100010],dp2[100010];
int main(){
    scanf("%lld",&n);
    for(long long i=1;i<=n;i++){
        scanf("%lld",&a1[i]);
        dp1[a1[i]]=i;
    }
    for(long long i=1;i<=n;i++)scanf("%lld",&a2[i]);
    for(long long i=1;i<=n;i++){
        if(dp1[a2[i]]>dp2[ans])dp1[++ans]=dp2[a2[i]];
        else{
            int l=0,r=ans,mid=0;
            while(l<r){
                mid=(l+r)/2;
                if(dp1[mid]<=dp2[a2[i]])l=mid+1;
                else r=mid;
            }
            dp2[l]=min(dp1[a2[i]],dp2[l]);
        }
    }
    printf("%lld",ans);
    return 0;
}

by ncwzdlsd @ 2023-05-07 20:02:28

@awaken1802 再检查一下 dp1dp2 有没有混用哦

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
long long n,ans,a1[100010],a2[100010],dp1[100010],dp2[100010];
int main(){
    scanf("%lld",&n);
    for(long long i=1;i<=n;i++){
        scanf("%lld",&a1[i]);
        dp1[a1[i]]=i;
    }
    for(long long i=1;i<=n;i++)scanf("%lld",&a2[i]);
    for(long long i=1;i<=n;i++){
        if(dp1[a2[i]]>dp2[ans])dp2[++ans]=dp1[a2[i]];
        else{
            int l=0,r=ans,mid=0;
            while(l<r){
                mid=(l+r)/2;
                if(dp1[a2[i]]<=dp2[mid])r=mid;
                else l=mid+1;
            }
            dp2[l]=min(dp1[a2[i]],dp2[l]);
        }
    }
    printf("%lld",ans);
    return 0;
}

by awaken1802 @ 2023-05-07 20:04:35

谢谢大佬!已关!


|