题目大意

有两个长度分别为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. 【hibernate】spring+ jpa + hibername 配置过程遇到的问题

    org.springframework.data.jpa.domain.support.AuditingEntityListener Auditing: 审计:查账Hades:Hades是一个开源库, ...

  2. NGUI系列教程六(技能冷却的CD效果)

    今天来学习一下,如何利用NGUI做技能冷却的CD效果. 1,首先按之前的教程Create Your UI,Camera为Simple 2D 最终如下图: 2,添加一个按钮,Background 为一张 ...

  3. Jfinal 入门

    Jfinal 入门 IDE----->IDEA 新建项目 新建web项目 添加maven特性 方便导入jar包,不用一个个导入了 配置pom.xml <dependencies> & ...

  4. objective_C 优缺点

    objective-c语言的优缺点 objc优点: 1) Cateogies 2) Posing3) 动态识别4) 指标计算5)弹性讯息传递6) 不是一个过度复杂的 C 衍生语言7) Objectiv ...

  5. swfupload 上传控件的配置

    第一:下载swfupload控件,点击下载: 第二,解压缩后,在项目中添加这四个文件,如图: 然后,在你的js文件中添加这段代码: /** * *定义swfupload配置文件对象 **/ var s ...

  6. Codeforces Round #329 div2

    Problem_A(593A): 题意: 给n个单词, 每个单词由小写字母组成, 且长度<=1000. 组成一篇文章的要求是: 所有单词所用字母 <= 2 即最多只能有两个不同的字母. 求 ...

  7. underscore vs jquery

    http://wordpress.tv/2013/06/23/k-adam-white-underscore-and-backbone-jquerys-new-friends/ http://www. ...

  8. android 自定义按钮的外边框

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  9. SPRING IN ACTION 第4版笔记-第五章BUILDING SPRING WEB APPLICATIONS-005-以path parameters的形式给action传参数(value=“{}”、@PathVariable)

    一 1.以path parameters的形式给action传参数 @Test public void testSpittle() throws Exception { Spittle expecte ...

  10. Rails3.2.3+ruby1.9.3 环境搭建,提示安全警告

    错误描述: 照着教程搭建了Rails的环境,能够正常运行,但是会但一个警告,如下: SECURITY WARNING: No secret option provided to Rack::Sessi ...