[luogu P1439] 【模板】最长公共子序列{最长上升子序列}

版权声明:请大家斧正,如喜欢的话,为拙见点一个赞吧。 https://blog.csdn.net/qq_39897867/article/details/82664298

题目

https://www.luogu.org/problemnew/show/P1439


解题思路

对于求 " L C S " "LCS" 问题的 O ( n ) O(n) 算法很容易可以得到,但是在本道**“模板题”**,这不是正解。
**注意题目给出的是:为自然数1-n的一个排列。**我们可以先将第一个数列离散化一下,设 b e l o n g [ x ] = i belong[x]=i ,原串则变成 1 3 , 2 2 , 3 1 , 4 4 , 5 5 1-3,2-2,3-1,4-4,5-5 ,对于第二个排列,我们可以直接求最长上升子序列即可。


代码

#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); 
}

猜你喜欢

转载自blog.csdn.net/qq_39897867/article/details/82664298
今日推荐