uva 10635-Prince and Princess 【LIS 求解 LCS】
来源:互联网 发布:windows 自带扫描软件 编辑:程序博客网 时间:2024/04/19 02:05
题意:给定两个分别具有q+1个和p+1个元素的序列,序列中的元素均不相同且均在1-n*n范围内。让你求解两个序列LCS的长度。
思路:n最大为250,n*n = 250 * 250 = 62500,LCS的O(n^2)算法会TLE。
先将一个序列里的元素建立val-id的映射fp,在另一个序列用fp[val]来替换val,然后求解LIS即可,为了方便求解,可以将fp[val] == 0的元素去掉。这样时间复杂度O(nlogn)。
AC代码:
#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#define INF 0x3f3f3f#define eps 1e-8#define MAXN (250*250+10)#define MAXM (100000)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while(a--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1using namespace std;int b[MAXN], s[MAXN];map<int, int> fp;int main(){ int t, n, p, q, kcase = 1; Ri(t); W(t) { Ri(n); Ri(p); Ri(q); p++; q++; int id = 0; fp.clear(); int a; for(int i = 0; i < p; i++) { Ri(a); fp[a] = ++id; } int top = 0; for(int i = 0; i < q; i++) { Ri(a); if(fp[a]) b[top++] = fp[a]; } int len = 1; s[0] = -1; for(int i = 0; i < top; i++) { s[len] = INF; int j = lower_bound(s, s+len, b[i]) - s; if(j == len) len++; s[j] = b[i]; } printf("Case %d: %d\n", kcase++, len-1); } return 0;}
0 0
- uva 10635-Prince and Princess 【LIS 求解 LCS】
- UVA 10635 Prince and Princess lcs--》lis
- Uva - 10635 - Prince and Princess(LCS转LIS)
- UVA 10635 Prince and Princess (LCS优化转LIS)
- UVa 10635 Prince and Princess / LCS 转 LIS
- uva 10635 Prince and Princess (将LCS 转化为 LIS)
- uva 10635Prince and Princess (LCS转LIS)
- UVA 10635 - Prince and Princess LCS转化为LIS
- UVA 10635 - Prince and Princess (LCS转成LIS DP)
- UVA - 10635 Prince and Princess LCS转LIS
- UVA - 10635 - Prince and Princess (LCS转化为LIS)
- UVA 10635 - Prince and Princess ( LCS 转换为LIS )
- UVa 10635 - Prince and Princess(LCS转LIS)
- uva-10635 Prince and Princess LCS转化为LIS
- UVA 10635 Prince and Princess【LCS 问题转换为 LIS】
- 【UVA 10635】【LCS转化成LIS】 Prince and Princess
- UVA 10635 Prince and Princess(LCS转LIS)
- UVA 10635 Prince and Princess LCS转化为LIS *
- 蓝桥杯 算法提高 逆序排列
- linkedin interview
- Java类的基础问题
- Java内存模型与并发同步常用的关键字(volatile和Synchronized)
- 栈(运算操作)
- uva 10635-Prince and Princess 【LIS 求解 LCS】
- 三种循环语句!及课后题
- 常用 Git 命令清单
- 使用VMware测试U盘启动盘是否制作成功
- 《剑指offer》——把字符串转换成整数
- Eclipse中开发导入Android系统内置应用
- Android Menu中android:showAsAction属性
- Google Closure Compiler开源的javascript压缩加密工具
- Android (默认)隐藏Menu Item