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. Android 图文教学让你彻底理解activity启动模式

    我们首先从最简单的开始, standard 这个模式就是默认的模式,我们都知道 当你用这个模式时,每次发送一个intent,都会生成一个新的实例! 我写一个简单的例子: <?xml versio ...

  2. 使用模板时 error LNK2019: 无法解析的外部符号

    类模板是c++编译器指令 说明了如何生成类和成员函数 除非编译器实现了新的关键字export关键字 否则将模板成员函数放置在一个独立的实现文件中 将无法运行 因为模板不是函数 他们不能单独编译 模板必 ...

  3. SWFUpload接受服务器Action返回的参数

    首先我们要了解这个函数 function uploadSuccess(file, serverData) { try { var progress = new FileProgress(file, t ...

  4. hihocoder 1356 分隔相同整数 简单贪心

    分析:考虑贪心,考虑填ans[i],前i-1个合法,现在剩下一些数, 那么挑出出现次数最多的数,次数为mx,当前剩余总数为sum 如果sum-mx>=mx-1那么肯定有解,这个想想就知道了(这种 ...

  5. 句柄(handle)

    < Back 句柄,在windows编程中用来标识: *.模块(module) *.任务(task) *.实例(instance) *.文件(file) *.内存块(block of memor ...

  6. js 判断字符是否以汉字开头

    javascript代码如下: var re = new RegExp("^[\u4e00-\u9fa5]"); if (re.test("aaa好")) { ...

  7. 字节顺序重置及“#include <algorith.h>”相关的STL最重要的头文件提醒

    这两天在写一个程序,需要将二进制文件中的数据以指定结构读入内存,说明文档中有提到大端序和小端序(Big Endian or Little Endian) 的概念,就找了一下字节顺序重置的算法,在一篇名 ...

  8. 关于WordPress建站的原理二三事

    在写关于仿站文章详情页如何制作之前,我觉得有必要就一些原理性的问题,做一些说明.文章详情页的核心模块和首页有很多相似的地方,比如调用文章的标题.文章的内容.文章分类.作者等,实现起来都差不多,因此,了 ...

  9. html5_common.js

    (function(){ this.sendAjax = function(url,func,formData,type){ type = type || "POST"; //默认 ...

  10. Codeforces Educational Codeforces Round 15 C. Cellular Network

    C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...