P1439 最长公共子序列 (LCS转LIS)

P1439 最长公共子序列 (LCS转LIS)

传送门

思路:离散化的好题。因为全排列的特性,可以将 a a 数组进行一个离散化,双射。映射为 1 , 2 , 3 , 4 n 1,2,3,4\dots n .然后题目就转化为求 b b 数组中的 L I S LIS ,然后用 O ( n l o g n ) O(nlogn) 求法就解决了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
#define mst(a) memset(a,0,sizeof a)
#define lx x<<1
#define rx x<<1|1
#define reg register
inline void read(int &x){ 
	x=0;int w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
	for(;ch>='0'&&ch<='9';ch=getchar())
		x=(x<<3)+(x<<1)+(ch&15);
	x*=w; 
}
int ans[N],b[N],n,x,cnt;
unordered_map<int,int>mp; 
int main(){
	read(n);
	for(reg int i=1;i<=n;i++) read(x),mp[x]=i;
	for(reg int i=1;i<=n;i++) read(b[i]),b[i]=mp[b[i]];
	ans[1]=b[1],cnt=1;
	for(reg int i=1;i<=n;i++){
		 if(b[i]>ans[cnt]) ans[++cnt]=b[i];
		 else {
		 	int p=lower_bound(ans+1,ans+cnt+1,b[i])-ans;
		 	ans[p]=b[i];
		 }
	}
	printf("%d\n",cnt);
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/weixin_45750972/article/details/106623142