UVA - 10635 Prince and Princess 转化为 LIS
2017-08-09 10:40
363 查看
首先每个序列的各个数字互不相同,可以给 a 串中的每个数字标一个编号(1 - n)
然后(把 b 中的每个数字也附上编号,)按照 a 中的数字所对应的编号,给 b 中每个数字附上相应的编号
这样 只需要求 b 编号序列的 最长上升子序列长度就好了
因为题中说了 第一个数字是1,b的第一个编号一定是1,所以不用担心 b 中出现而a 中没有的数字编号为0 会影响结果
代码如下:
然后(把 b 中的每个数字也附上编号,)按照 a 中的数字所对应的编号,给 b 中每个数字附上相应的编号
这样 只需要求 b 编号序列的 最长上升子序列长度就好了
因为题中说了 第一个数字是1,b的第一个编号一定是1,所以不用担心 b 中出现而a 中没有的数字编号为0 会影响结果
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<set> #include<stack> #include<queue> #include<algorithm> // cout << " === " << endl; using namespace std; typedef long long ll; const int maxn = 100000 + 7, INF = 0x3f3f3f3f, mod = 1e9+7; int T, n, p, q, ans; int a[maxn], b[maxn], c[maxn], d[maxn]; void init() { memset(a, 0, sizeof a); memset(b, 0, sizeof b); memset(c, 0, sizeof c); for(int i = 1; i <= p; ++i) scanf("%d", &a[i]); for(int i = 0; i < q; ++i) scanf("%d", &b[i]); for(int i = 1; i <= p; ++i) c[a[i]] = i; for(int i = 0; i < q; ++i) b[i] = c[b[i]]; } void solve() { memset(d, INF, sizeof d); for(int i = 0; i < q; ++i) *lower_bound(d, d+q+1, b[i]) = b[i]; int ans = lower_bound(d, d+q+1, INF) - d; printf("%d\n", ans); } int main() { scanf("%d", &T); int kase = 1; while(T--) { scanf("%d%d%d", &n, &p, &q); p++, q++; init(); printf("Case %d: ", kase++); solve(); } return 0; }
相关文章推荐
- dp(LCS转化成LIS)uva 10635 - Prince and Princess
- UVA 10635 Prince and Princess LCS转化为LIS *
- uva 10635 Prince and Princess(LCS问题转化成LIS问题O(nlogn))
- 【UVA 10635】【LCS转化成LIS】 Prince and Princess
- 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 ( LIS )
- UVa 10635 - Prince and Princess(LCS转LIS)
- uva 10635 - Prince and Princess lcs->lis 2018-2-8
- 一中OJ #1434 序列的LCS [UVa 10635 -> Prince and Princess] | 动态规划 LCS映射转换LIS | 解题报告
- Prince and Princess UVA - 10635(LCS转LIS)
- UVa 10635 Prince and Princess / LCS 转 LIS
- UVA 10635 Prince and Princess——LIS
- uva 10635 Prince and Princess(LCS成问题LIS问题O(nlogn))
- UVA - 10635 Prince and Princess LCS转LIS
- UVa 10635 Prince and Princess 【LIS】
- Uva - 10635 - Prince and Princess(LCS转LIS)
- uva 10635-Prince and Princess 【LIS 求解 LCS】