这题朴素DP是(O(n^2))的,会被(10^5)卡掉qwq
考虑到两个排列是1~n的全排列,所以离散化第一个排列,然后求一下第二个排列的二分优化最长不降子序列就好了orzzz
CODE:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int a1[100010],a2[100010];
int belong[100010];
int f[100010],b[100010],len;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a1[i]);
belong[a1[i]]=i;
}
for(int i=1;i<=n;i++)
scanf("%d",&a2[i]);
for(int i=1;i<=n;i++)
{
if(belong[a2[i]]>b[len])
{
b[++len]=belong[a2[i]];
f[i]=len;
continue;
}
int k=lower_bound(b+1,b+len+1,belong[a2[i]])-b;
b[k]=belong[a2[i]];
f[i]=k;
}
printf("%d
",len);
return 0;
}