dfsMLE50求调

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

cgxd @ 2024-07-09 20:40:02

#include<bits/stdc++.h>
using namespace std;
int n, ans[1005];
vector<int> G[1005];
int pos[3][1005], res;
int dfs(int x){
    if(ans[x])
        return ans[x];
    ans[x] = 1;
    for(int i: G[x])
        ans[x] = max(dfs(i) + 1, ans[x]);
    return ans[x];
}signed main(){
    scanf("%d", &n);
    for(int i = 1; i <= 2; ++i){
        for(int j = 1; j <= n; ++j){
            int a;
            scanf("%d", &a);
            pos[i][a] = j;
        }
    }for(int j = 1; j <= n; ++j){
        for(int l = 1; l <= n; ++l){
            for(int i = 1; i <= 2; ++i){
                if(pos[i][j] >= pos[i][l])
                    break;
                if(i == 2)
                    G[j].push_back(l);
            }
        }
    }for(int i = 1; i <= n; ++i)
        res = max(res, dfs(i));
    printf("%d", res);
    return 0;
}

by Ace_FutureDream @ 2024-07-09 20:42:36

@cgxd 温馨提示:n<=10^5


|