版权声明:请大家斧正,如喜欢的话,为拙见点一个赞吧。 https://blog.csdn.net/qq_39897867/article/details/82664298
题目
https://www.luogu.org/problemnew/show/P1439
解题思路
对于求
问题的
算法很容易可以得到,但是在本道**“模板题”**,这不是正解。
**注意题目给出的是:为自然数1-n的一个排列。**我们可以先将第一个数列离散化一下,设
,原串则变成
,对于第二个排列,我们可以直接求最长上升子序列即可。
代码
#include<cstdio>
#include<algorithm>
using namespace std;
const int inf=1e5+1;
int b[inf],belong[inf],len,n;
int main()
{
scanf("%d",&n); int x;
for (int i=1;i<=n;i++) scanf("%d",&x),belong[x]=i;
for (int i=1;i<=n;i++)
{
scanf("%d",&x);
if (belong[x]>b[len]) b[++len]=belong[x];
else b[lower_bound(b+1,b+len+1,belong[x])-b]=belong[x];
}
printf("%d",len);
}