题目描述
给出1-n的两个排列P1和P2,求它们的最长公共子序列。
输入输出格式
输入格式:
第一行是一个数n,
接下来两行,每行为n个数,为自然数1-n的一个排列。
输出格式:
一个数,即最长公共子序列的长度
输入输出样例
输入样例#1:
5 3 2 1 4 5 1 2 3 4 5
输出样例#1:
3
说明
【数据规模】
对于50%的数据,n≤1000
对于100%的数据,n≤100000
因为每个数都是1到n的排列所以我们建立一个映射
把第一个序列对应为1-n的数这样得到的1-n是严格上升的
而上升这个性质就可以转化为求LIS(这样也需要将第二个序映射为另一个数)
而求LIS就可以用nlogn的算法了
QWQ:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn=1e5+5; int a[maxn]; int b[maxn]; int c[maxn]; int arc[maxn]; int ans=1; int n; int main(){ cin>>n; for(int i=1;i<=n;i++){cin>>a[i];arc[a[i]]=i;} for(int i=1;i<=n;++i){cin>>b[i];} for(int i=1;i<=n;++i){c[i]=arc[b[i]];} memset(b,0,sizeof b); for(int i=1;i<=n;++i){ int pos=lower_bound(b+1,b+1+ans,c[i])-b; ans=max(pos,ans); b[pos]=c[i]; } cout<<ans-1<<endl; return 0; }