UVa 10635 王子和公主(LCS转LIS)
2017-05-30 09:58
295 查看
https://vjudge.net/problem/UVA-10635
题意:
有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n^2之间的整数。两个序列的第一个元素均为1,。求出A和B的最长公共子序列长度。
思路:
因为序列中元素各不相同,所以我们可以把A重新编号为{1,2,3,4,5...},也就是相当于映射,之后B根据A映射,最后在B中找LIS即可。
题意:
有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n^2之间的整数。两个序列的第一个元素均为1,。求出A和B的最长公共子序列长度。
思路:
因为序列中元素各不相同,所以我们可以把A重新编号为{1,2,3,4,5...},也就是相当于映射,之后B根据A映射,最后在B中找LIS即可。
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<vector> #include<stack> #include<queue> #include<cmath> #include<map> using namespace std; typedef long long LL; typedef pair<int,int> pll; const int INF=0x3f3f3f3f; const int maxn=250*250; int n,p,q; int num[maxn],s[maxn]; int d[maxn],g[maxn]; int main() { //freopen("D:\\input.txt","r",stdin); int kase=0; int T; scanf("%d",&T); while(T--) { memset(num,0,sizeof(num)); scanf("%d%d%d",&n,&p,&q); for(int i=1;i<=p+1;i++) { int x; scanf("%d",&x); num[x]=i; } int cnt=0; for(int i=0;i<q+1;i++) { int x; scanf("%d",&x); if(num[x]) s[cnt++]=num[x]; } int ans=0; for(int i=1;i<=cnt;i++) g[i]=INF; for(int i=0;i<cnt;i++) { int k=lower_bound(g+1,g+cnt+1,s[i])-g; d[i]=k; g[k]=s[i]; ans=max(ans,d[i]); } printf("Case %d: %d\n",++kase,ans); } return 0; }
相关文章推荐
- UVa 10635(lcs转lis优化模板)王子和公主
- LCS,LIS(王子和公主,uva 10635)
- (UVA - 10635)Prince and Princess (LCS变形为LIS,DP)
- Prince and Princess UVA - 10635(LCS转LIS)
- UVA 10635 Prince and Princess(LCS转LIS)
- uva 10635-Prince and Princess 【LIS 求解 LCS】
- dp(LCS转化成LIS)uva 10635 - Prince and Princess
- UVA - 10635 - Prince and Princess (LCS转化为LIS)
- Uva 10635 Prince and Princess (LCS变形LIS)
- UVA 10635 - Prince and Princess (LCS转成LIS DP)
- UVA 10635 王子和公主
- uva-10635 Prince and Princess LCS转化为LIS
- UVa 10635 Prince and Princess / LCS 转 LIS
- UVA-10635 LCS转化为LIS
- 【UVA 10635】【LCS转化成LIS】 Prince and Princess
- UVA 10635 Prince and Princess LCS转化为LIS *
- uva 10635 LCS转LIS
- uva 10635 Prince and Princess(LCS问题转化成LIS问题O(nlogn))
- uva10635 王子和公主(把lcs转化为lis)
- Prince and Princess UVA 10635 王子和公主 nlogn 求最长上升子序的长度