UVA 10635 Prince and Princess
2013-06-04 12:43
381 查看
这题很明显的是LCS,不过p, q太大,普通的O(n ^ 2 )算法显然会超时
这里可以把LCS问题转换成LIS问题,LIS有nlogn的算法,然后复杂度就下来了.
因为每个序列都是没重复的,所以可以把序列1重新编号成1 - p + 1,然后序列2按照序列1的数字的对应编号进行转换.
例如:
序列1{1 7 5 4 8 3 9} 编号后变成{1 2 3 4 5 6 7}
序列2{1 4 3 5 6 2 8 9}转换后变成{1 4 6 0 3 0 0 5 7}
去掉转换后的序列2中的0,因为王子没有踩到这个格子.
对转换后的序列2求LIS就可以了.
这里可以把LCS问题转换成LIS问题,LIS有nlogn的算法,然后复杂度就下来了.
因为每个序列都是没重复的,所以可以把序列1重新编号成1 - p + 1,然后序列2按照序列1的数字的对应编号进行转换.
例如:
序列1{1 7 5 4 8 3 9} 编号后变成{1 2 3 4 5 6 7}
序列2{1 4 3 5 6 2 8 9}转换后变成{1 4 6 0 3 0 0 5 7}
去掉转换后的序列2中的0,因为王子没有踩到这个格子.
对转换后的序列2求LIS就可以了.
#include <cstdio> #include <algorithm> #include <iostream> #include <memory.h> using namespace std; const int maxn = 62555; int tb[maxn]; int A[maxn], d[maxn], g[maxn]; int p, q; int main(){ int T, trash, cas = 1; scanf("%d", &T); while(T--){ scanf("%d%d%d", &trash, &p, &q); memset(tb, 0, sizeof(tb)); for(int i = 0; i < p + 1; ++i){ int t = 0; scanf("%d", &t); tb[t] = i + 1; } int n = 0, ans = 0; for(int i = 0; i < q + 1; ++i){ int t; scanf("%d", &t); if(tb[t]){ A[n++] = tb[t]; } } memset(g, 0X20, sizeof(g)); for(int i = 0; i < n; ++i){ int k = lower_bound(g + 1, g + n + 1, A[i]) - g; d[i] = k; g[k] = A[i]; ans = max(ans, d[i]); } printf("Case %d: %d\n", cas++, ans); } return 0; }
相关文章推荐
- Uva 10635 - Prince and Princess(最长公共子序列 nlogn 算法)
- UVa 10635 - Prince and Princess
- uva_10635 - Prince and Princess(LIS)
- UVa 10635 - Prince and Princess 问题转化..LCS巧妙算法..
- UVA - 10635 Prince and Princess LCS转LIS
- UVA 10635 Prince and Princess DP LIS
- Prince and Princess UVA 10635 王子和公主 nlogn 求最长上升子序的长度
- UVA 10635 Prince and Princess DP LIS
- UVA - 10635 Prince and Princess 转化为 LIS
- uva 10635 Prince and Princess(LCS问题转化成LIS问题O(nlogn))
- UVA 10635 Prince and Princess 递推
- UVA 10635 Prince and Princess
- Uva - 10635 - Prince and Princess(LCS转LIS)
- Prince and Princess UVA 10635 王子和公主 nlogn 求最长上升子序的长度
- UVA 10635 Prince and Princess——LIS
- UVA 10635--Prince and Princess+nlgn求最长公共子序列
- Prince and Princess UVA - 10635
- Prince and Princess - UVa 10635 dp
- UVA 10635 Prince and Princess(最长公共子序列)
- UVa -- 10635 Prince and Princess 【想法】