题目大意

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

题解

这个是大白书上的例题,不过这题真的很好,很考验思维。因为p和q都是250^2=62500,如果用LCS的话时间复杂度是O(pq),显然会超时。。。。不过这题的两个序列很特殊,就是没有重复的元素,这样可以把A中的元素重新编号为1~p+1。然后B根据A也重新编号,这样,新的A和B的LCS实际上就是新的B的LIS。LIS可以在O(nlogn)时间内解决~~~

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAXN 255*255
int a[MAXN],g[MAXN];
int num[MAXN];
int main()
{
int n,p,q,T,cnt,t=0;
scanf("%d",&T);
while(T--)
{
memset(num,0,sizeof(num));
scanf("%d%d%d",&n,&p,&q);
cnt=0;
for(int i=1;i<=p+1;i++)
{
int x;
scanf("%d",&x);
num[x]=i;
}
for(int i=0;i<=q;i++)
{
int x;
scanf("%d",&x);
if(num[x])
a[cnt++]=num[x];
}
g[0]=a[0];
int len=0;
for(int i=1;i<cnt;i++)
if(a[i]>g[len])g[++len]=a[i];
else
{
int pos=lower_bound(g,g+len,a[i])-g;
g[pos]=a[i];
}
printf("Case %d: %d\n",++t,len+1);
}
return 0;
}

UVa10635 - Prince and Princess(LCS转LIS)的更多相关文章

  1. UVA - 10635 Prince and Princess LCS转LIS

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

  2. uva 10635 - Prince and Princess(LCS)

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

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

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

  4. Uva 10635 - Prince and Princess LCS/LIS

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

  5. Uva10635 Prince and Princess

    题目戳这里 这题如果用\(f_{i,j}\)这样dp的话肯定过不了,必须另辟蹊径.题目说了数字不重复.我们先只留下两个数组共有的数字.然后我们处理出这样一个数组\(S\),\(S_i\)表示\(A_i ...

  6. 【线型DP】【LCS】UVA_10635 Prince and Princess

    嘤嘤嘤,我又来了,刚A完就写,这个沙雕题有丶恶心.                  ???时间4.11发现所有表情包都莫得了 题目: In an n×n chessboard, Prince and ...

  7. UVa10653.Prince and Princess

    题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. UVA 10653.Prince and Princess

    题目 In an n * n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbe ...

  9. 强连通+二分匹配(hdu4685 Prince and Princess)

    Prince and Princess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

随机推荐

  1. <a>作Form表单提</a>

    1. Form表单<%using (Html.BeginForm("AddRoles", "RoleManage", FormMethod.Post, n ...

  2. myeclipse10.0优化

    一.Myeclipse10修改字体  MyEclipse10是基于Eclipse3.7内核,但在Eclipse的Preferences-〉general-〉 Appearance->Colors ...

  3. ExtJS4加载FormPanel数据的几种方式

    我们做web应用最多的就是处理表单,extjs为我们提供了很多处理表单的功能,很多初学者疑惑怎么加载表单数据,到底能用什么方式加载?本文中,我将我自己实验过的进行一下总结,自己备忘,也希望能帮助到其他 ...

  4. DB天气app冲刺第四天

    今天卡壳了 做得很慢.. 明天继续 换一种思路试一下吧..

  5. BZOJ 3715: [PA2014]Lustra

    Description Byteasar公司专门外包生产带有镜子的衣柜.刚刚举行的招标会上,有n个工厂参加竞标.所有镜子都是长方形的,每个工厂能够制造的镜子都有其各自的最大.最小宽度和最大.最小高度. ...

  6. 浅谈HTTP响应拆分攻击

    在本文中,我们将探讨何谓HTTP响应拆分以及攻击行为是怎样进行的.一旦彻底理解了其发生原理(该原理往往被人所误解),我们就可以探究如何利用响应拆分执行跨站点脚本(简称XSS).接下来自然就是讨论如果目 ...

  7. unity 引用 移动mm 支付sdk

    原地址:http://blog.csdn.net/u012085988/article/details/17531005 资源下载:http://download.csdn.net/detail/u0 ...

  8. eCos驱动分析 之 ISR是如何与硬件中断联系起来的?

    http://keendawn.blog.163.com/blog/static/8888074320116205833478/

  9. linux如何安装jdk

    一.安装 创建安装目录,在/usr/java下建立安装路径,并将文件考到该路径下: # mkdir /usr/java 1.jdk-6u11-linux-i586.bin 这个是自解压的文件,在lin ...

  10. JAVA与C++的区别和联系

    这篇总结的貌似不错: http://wenku.baidu.com/link?url=VixkWGl0BzUkmceaDJnQeUhzKEIex6poGaKKvMTP87P8a7HTmS5uIi87I ...