#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#definergregisterusingnamespace std;typedeflonglong ll;inlineintsread(){int x=0,f=1;char c=getchar();while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}return f*x;}constint maxn=3010;int n;int a[maxn],b[maxn];int dp[maxn][maxn];intmain(){
n=sread();for(rg int i=1;i<=n;++i) a[i]=sread();for(rg int i=1;i<=n;++i) b[i]=sread();for(rg int i=1;i<=n;++i)for(rg int j=1;j<=n;++j){if(a[i]==b[j]){for(rg int k=0;k<j;k++){if(b[k]<a[i]) dp[i][j]=max(dp[i][j],dp[i-1][k]+1);}}else dp[i][j]=dp[i-1][j];}int ans=0;for(rg int i=1;i<=n;++i){
ans=max(ans,dp[n][i]);}
cout<<ans<<endl;return0;}
优化版
#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#definergregisterusingnamespace std;typedeflonglong ll;inlineintsread(){int x=0,f=1;char c=getchar();while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}return f*x;}constint maxn=3010;int n;int a[maxn],b[maxn];int dp[maxn][maxn];intmain(){
n=sread();for(rg int i=1;i<=n;++i) a[i]=sread();for(rg int i=1;i<=n;++i) b[i]=sread();for(rg int i=1;i<=n;++i){int val=0;if(b[0]<a[i]) val=dp[i-1][0];for(rg int j=1;j<=n;++j){if(a[i]==b[j]) dp[i][j]=val+1;else dp[i][j]=dp[i-1][j];if(b[j]<a[i]) val=max(val,dp[i-1][j]);}}int ans=0;for(rg int i=1;i<=n;++i){
ans=max(ans,dp[n][i]);}
cout<<ans<<endl;return0;}