Problem D
Prince and Princess
Input: 
Standard Input

Output: Standard Output

Time Limit: 3 Seconds

In an n x n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbered 1, 2, 3 ... n*n, as shown below:

Prince stands in square 1, make p jumps and finally reach square n*n. He enters a square at most once. So if we use xp to denote the p-th square he enters, then x1, x2, ... xp+1 are all different. Note that x1 = 1 and xp+1 = n*n. Princess does the similar thing - stands in square 1, make q jumps and finally reach square n*n. We use y1, y, ... yq+1 to denote the sequence, and all q+1 numbers are different.

Figure 2 belows show a 3x3 square, a possible route for Prince and a different route for Princess.

The Prince moves along the sequence: 1 --> 7 --> 5 --> 4 --> 8 --> 3 --> 9 (Black arrows), while the Princess moves along this sequence: 1 --> 4 --> 3 --> 5 --> 6 --> 2 --> 8 --> 9 (White arrow).

The King -- their father, has just come. "Why move separately? You are brother and sister!" said the King, "Ignore some jumps and make sure that you're always together."

For example, if the Prince ignores his 2nd, 3rd, 6th jump, he'll follow the route: 1 --> 4 --> 8 --> 9. If the Princess ignores her 3rd, 4th, 5th, 6th jump, she'll follow the same route: 1 --> 4 --> 8 --> 9, (The common route is shown in figure 3) thus satisfies the King, shown above. The King wants to know the longest route they can move together, could you tell him?

Input

The first line of the input contains a single integer t(1 <= t <= 10), the number of test cases followed. For each case, the first line contains three integers n, p, q(2 <= n <= 250, 1 <= p, q < n*n). The second line contains p+1 different integers in the range [1..n*n], the sequence of the Prince. The third line contains q+1 different integers in the range [1..n*n], the sequence of the Princess.

Output

For each test case, print the case number and the length of longest route. Look at the output for sample input for details.

 

Sample Input                           Output for Sample Input

1

3 6 7

1 7 5 4 8 3 9

1 4 3 5 6 2 8 9

Case 1: 4

 
#include<cstdio>
#include<algorithm>
#define INF 100000000
#define max 65000
int a[max],s[max],g[max],d[max];
int main()
{
int t,N,p,q,i,x,count=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&N,&p,&q);
for(i=1;i<=p+1;i++)
{
scanf("%d",&x);
a[x]=i;
}
int n=0;
for(i=0;i<q+1;i++)
{
scanf("%d",&x);
if(a[x])
s[n++]=a[x];
}
for(i=1;i<=n;i++) g[i]=INF;
int ans=0;
for(i=0;i<n;i++)
{
int k=std::lower_bound(g+1,g+n+1,s[i])-g;
d[i]=k;
g[k]=s[i];
ans=ans>d[i]?ans:d[i];
}
printf("Case %d: %d\n",count++,ans);
}
return 0;
}

10635 - Prince and Princess的更多相关文章

  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

    讲一下题目大意,就是有两个长度为p + 1和q + 1的序列,求它们的LCS. 如果用O(pq)的算法对于这道题来说还是太慢了.所以要另外想一些方法.注意到序列中的所有元素都不相同,所以两个序列中数对 ...

  4. UVa 10635 Prince and Princess - 动态规划

    讲一下题目大意,就是有两个长度为p + 1和q + 1的序列,求它们的LCS. 如果用O(pq)的算法对于这道题来说还是太慢了.所以要另外想一些方法.注意到序列中的所有元素都不相同,所以两个序列中数对 ...

  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

    题意描述:有两个长度分别为p+1和q+1的序列,每个元素中的各个元素互不相同.都是1~n^2之间的整数,求A和B的最长公共子序列.(2<=n<=250,1<=p,q<=n^2) ...

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

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

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

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

  9. [UVA 10635] Prince ans Princess

    图片加载可能有点慢,请跳过题面先看题解,谢谢 这道题... 还是要点思维的... 第一眼看是个最长公共子序列,但是, \(N\le 62500\) ,并不能 \(O(n^2)\) 求 $ $ 这道题有 ...

随机推荐

  1. in 和 exist 区别

    select * from Awhere id in(select id from B) 以上查询使用了in语句,in()只执行一次,它查出B表中的所有id字段并缓存起来.之后,检查A表的id是否与B ...

  2. android定时三种方式

    一.采用Handler与线程的sleep(long)方法二.采用Handler的postDelayed(Runnable, long)方法三.采用Handler与timer及TimerTask结合的方 ...

  3. [摘]string.jion() 解决办法

    for (int j = 1; j <= 5; j++)            {                List<int> list = new List<int&g ...

  4. HTTP协议的状态码

    对于Web编程人员来说,熟悉了解HTTP协议的状态码是很有必要的,很多时侯可能根据HTTP协议的状态码很快就能定位到错误信息!今天整理了一下所有HTTP状态码. HTTP状态码(HTTP Status ...

  5. 任务(Tasks)

    在Eclipse中用TODO标签管理任务,利用这个功能可以方便地将项目中一些需要处理的任务记录下来.我们可以通过在java注释里添加任务标签来标记一个任务,任务可以通过Tasks(任务)视图来察看. ...

  6. 【转】Linux Page Cache的工作原理

    1 .前言 自从诞生以来,Linux 就被不断完善和普及,目前它已经成为主流通用操作系统之一,使用得非常广泛,它与Windows.UNIX 一起占据了操作系统领域几乎所有的市场份额.特别是在高性能计算 ...

  7. Android开源图表库介绍

    XCL-Charts XCL-Charts V1.8     Android开源图表库(XCL-Charts is a free charting library for Android platfo ...

  8. 捣蛋phpwind过滤器执行流程

    从上一篇我们就大概就知道过滤器的定义和怎样去配置,这一节来说说执行流程 public function run($handlerAdapter = null) { $handlerAdapter != ...

  9. Mapreduce读取Hbase表,写数据到一个Hbase表中

    public class LabelJob { public static void main(String[] args) throws Exception { Job job = Job.getI ...

  10. IOS中 什么是UITableView的索引放大镜字符

    IOS中 什么是UITableView的索引放大镜字符 [_dataSource addObject:UITableViewIndexSearch]; 版权声明:本文为博主原创文章,未经博主允许不得转 ...