题意:有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n2的整数。两个序列的第一个元素均为1。求出A和B的最长公共子序列长度。

分析:

A = {1,7,5,4,8,3,9},B = {1,4,3,5,6,2,8,9}。

1、A中元素各不相同,因此将A中序列重新编号为1~p+1,即A中每个元素在A中是第几个出现的。

2、按照A中制定的编号原则给B重新编号,则B为{1,4,6,3,0,0,5,7},0表示这些元素在A中没有出现过。

3、新的A和B的LCS实际上就是新的B的LIS。LIS可在O(nlogn)内解决。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 62500 + 10;
const int MAXT = 100000 + 10;
using namespace std;
int a[MAXN], b[MAXN], dp[MAXN];
map<int, int> mp;
int main(){
int T;
scanf("%d", &T);
int kase = 0;
while(T--){
mp.clear();
int n, p, q;
scanf("%d%d%d", &n, &p, &q);
for(int i = 1; i <= p + 1; ++i){
scanf("%d", &a[i]);
mp[a[i]] = i;
}
for(int i = 1; i <= q + 1; ++i){
scanf("%d", &b[i]);
if(!mp.count(b[i])){
b[i] = 0;
}
else{
b[i] = mp[b[i]];
}
}
memset(dp, INT_INF, sizeof dp);
for(int i = 1; i <= q + 1; ++i){
*lower_bound(dp, dp + q + 1, b[i]) = b[i];
}
printf("Case %d: %d\n", ++kase, lower_bound(dp, dp + q + 1, INT_INF) - dp);
}
return 0;
}

  

UVA - 10635 Prince and Princess(LCS,可转化为LIS)的更多相关文章

  1. uva 10635 - Prince and Princess(LCS)

    题目连接:10635 - Prince and Princess 题目大意:给出n, m, k,求两个长度分别为m + 1 和 k + 1且由1~n * n组成的序列的最长公共子序列长的. 解题思路: ...

  2. UVA - 10635 Prince and Princess LCS转LIS

    题目链接: http://bak.vjudge.net/problem/UVA-10635 Prince and Princess Time Limit: 3000MS 题意 给你两个数组,求他们的最 ...

  3. Uva 10635 - Prince and Princess LCS/LIS

    两个长度分别为p+1和q+1的由1到n2之前的整数组成的序列,每个序列的元素各不相等,两个序列第一个元素均为1.求两个序列的最长公共子序列 https://uva.onlinejudge.org/in ...

  4. UVA 10635 - Prince and Princess LCS转化为LIS

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  5. UVA 10635 Prince and Princess【LCS 问题转换为 LIS】

    题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19051 题意: 有两个长度分别为p+1和q+1的由1到n2 ...

  6. UVA 10635 Prince and Princess—— 求LCS(最长公共子序列)转换成 求LIS(最长递增子序列)

    题目大意:有n*n个方格,王子有一条走法,依次经过m个格子,公主有一种走法,依次经过n个格子(不会重复走),问他们删去一些步数后,重叠步数的最大值. 显然是一个LCS,我一看到就高高兴兴的打了个板子上 ...

  7. Uva 10635 - Prince and Princess 问题转化,元素互不相同(在自身序列中独特)的两个数列的LCS,LIS 难度: 2

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  8. uva 10635 Prince and Princess(LCS成问题LIS问题O(nlogn))

    标题效果:有两个长度p+1和q+1该序列.的各种元素的每个序列不是相互同.并1~n^2之间的整数.个序列的第一个元素均为1. 求出A和B的最长公共子序列长度. 分析:本题是LCS问题,可是p*q< ...

  9. Uva 10635 Prince and Princess (LCS变形LIS)

    直接LCS是时间复杂度是O(p*q)的,但是序列元素各不相同,只要把其中一个序列映射成有序的, 另外一个序列再做相同的映射,没有的直接删掉,就变成了求另一个序列LIS. #include<bit ...

随机推荐

  1. 【PAT甲级】1032 Sharing (25 分)

    题意: 输入两个单词的起始地址和一个正整数N(<=1e5),然后输入N行数据,每行包括一个五位数的字母地址,字母和下一个字母的地址.输出这两个单词的公共后缀首字母的地址,若无公共后缀则输出-1. ...

  2. CSP2019 Emiya 家今天的饭

    Description: 有 \(n\) 中烹饪方法和 \(m\) 种食材,要求: 至少做一种菜 所有菜的烹饪方法各不相同 同种食材的菜的数量不能超过总菜数的一半 求做菜的方案数. Solution1 ...

  3. matplotlib 柱状图 Bar Chart 样例及参数

    def bar_chart_generator():     l = [1,2,3,4,5]     h = [20, 14, 38, 27, 9]     w = [0.1, 0.2, 0.3, 0 ...

  4. VMware 设置共享文件夹

    1. 打开: 虚拟机 -> 设置 -> 选项 2. 选择 “总是启用” ,然后点 “添加” 选择你要共享的本地文件夹,最后点确定. 3. Linux下在 /mnt/hgfs 文件夹下就可以 ...

  5. sqlalchemy 连接mysql8.0报 RuntimeError: cryptograpy si requeired for sha256_password 错误

    cryptography is required for sha256_password or caching_sha2_password 需要cryptography模块的支持才能连接需要sha25 ...

  6. Day11 - N - Game HDU - 3389

    题目链接 题意是说有1到n个标号的盒子,选择一个非空的盒子A,B是否空无所谓,满足(A+B)%2=1,(A+B)%3=0,A>B 解上面的同余方程组,最小解为3,循环为2*3=6,那我们可以把前 ...

  7. 「JSOI2010」满汉全席

    前言 由于蒟蒻才刚开始学 \(\text{2-SAT}\),所以题解中有的地方可能不够精炼,望多包涵! 题目描述 题目意思很简单,标准的\(\text{2-SAT}\)问题模型.那么我们就先来介绍一下 ...

  8. 笔记||Pyhthon3进阶之多线程操作共享数据

    # 多线程操作共享数据--------------------------------------------------------------- # import threading# 使用锁# ...

  9. C#中struct和class的区别详解 (转载)

    本文详细分析了C#中struct和class的区别,对于C#初学者来说是有必要加以了解并掌握的. 简单来说,struct是值类型,创建一个struct类型的实例被分配在栈上.class是引用类型,创建 ...

  10. Ubuntu 16.04 编译安装&&用dpkg安装--zabbix3.4

    编译安装zabbix3.4,官方有教程,但是遇到一些报错: 总结下来,需要安装这些环境: 更新系统: apt update apt upgrade 先装这些: apt-get install libm ...