10635 - Prince and Princess
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, y2 , ... 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的更多相关文章
- uva 10635 - Prince and Princess(LCS)
题目连接:10635 - Prince and Princess 题目大意:给出n, m, k,求两个长度分别为m + 1 和 k + 1且由1~n * n组成的序列的最长公共子序列长的. 解题思路: ...
- UVA - 10635 Prince and Princess LCS转LIS
题目链接: http://bak.vjudge.net/problem/UVA-10635 Prince and Princess Time Limit: 3000MS 题意 给你两个数组,求他们的最 ...
- [题解]UVa 10635 Prince and Princess
讲一下题目大意,就是有两个长度为p + 1和q + 1的序列,求它们的LCS. 如果用O(pq)的算法对于这道题来说还是太慢了.所以要另外想一些方法.注意到序列中的所有元素都不相同,所以两个序列中数对 ...
- UVa 10635 Prince and Princess - 动态规划
讲一下题目大意,就是有两个长度为p + 1和q + 1的序列,求它们的LCS. 如果用O(pq)的算法对于这道题来说还是太慢了.所以要另外想一些方法.注意到序列中的所有元素都不相同,所以两个序列中数对 ...
- UVA 10635 Prince and Princess【LCS 问题转换为 LIS】
题目链接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19051 题意: 有两个长度分别为p+1和q+1的由1到n2 ...
- UVA 10635 Prince and Princess
题意描述:有两个长度分别为p+1和q+1的序列,每个元素中的各个元素互不相同.都是1~n^2之间的整数,求A和B的最长公共子序列.(2<=n<=250,1<=p,q<=n^2) ...
- uva 10635 Prince and Princess(LCS成问题LIS问题O(nlogn))
标题效果:有两个长度p+1和q+1该序列.的各种元素的每个序列不是相互同.并1~n^2之间的整数.个序列的第一个元素均为1. 求出A和B的最长公共子序列长度. 分析:本题是LCS问题,可是p*q< ...
- Uva 10635 - Prince and Princess 问题转化,元素互不相同(在自身序列中独特)的两个数列的LCS,LIS 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- [UVA 10635] Prince ans Princess
图片加载可能有点慢,请跳过题面先看题解,谢谢 这道题... 还是要点思维的... 第一眼看是个最长公共子序列,但是, \(N\le 62500\) ,并不能 \(O(n^2)\) 求 $ $ 这道题有 ...
随机推荐
- java-No exception of type ConfigurationException can be thrown; an exception type must be a subclass of Throwable
功能:读配置文件 java菜鸟:导入工程在报名处就开始报错,第一次遇到 import org.apache.commons.lang3.StringUtils; import org.apache.c ...
- nested push animation can result in corrupted navigation bar
2013-12-06 10:15:51.668 CodingForFun[4569:70b] nested push animation can result in corrupted navigat ...
- java批量插入或更新的问题
在批量插入或者更新中,setXXX的时候字段类型必须一致.例如:在普通sql中 pstmt8.setBigDecimal(j ,xxx);可以写成pstmt8.setString(j,xxx.toSt ...
- 康乐不风流之爱解题的pde灌水王张祖锦
康乐不风流之爱解题的pde灌水王张祖锦 师弟: 邓洪存 (现在烟台大学任教) 好吧, 我一直想写写康乐园里与我相熟的这几个人, 不如趁此机会开始. 第一批人物为张祖锦.苏延辉.张会春.黄显涛.刘兴兴. ...
- ansibleplaybook的使用
1.简单格式要求 [root@ansibleserver ansible]# cat nagios.yml --- - hosts: nagiosserver tasks: - name: ensur ...
- 封装cookie.js、EventUtil.js、
最近学习了javascript,封装好的东西看起来舒服,以备需要的时候拉出来,jquery对javascript做了很好的封装!以后会多用jquery多些 var CookieUtil = { get ...
- 去掉StringBuilder或String 最后一个项逗号
一. sb.Length = sb.Length - 1; 二. stringBuilder.Remove(stringBuilder.ToString().LastIndexOf(','), 1); ...
- 轻松学习Linux之自动执行任务
在 Linux 中,任务可以被配置在指定的时间段.指定的日期.或系统平均载量低于指定的数量时自动运行,系统管理员可使用自动化的任务来执行定期备份.监控系统.运行定制脚本等,往往初学者都是win用户,习 ...
- openjdk sunjdk区别
使用过LINUX的人都应该知道,在大多数LINUX发行版本里,内置或者通过软件源安装JDK的话,都是安装的openjdk,那么到底什么是openjdk,它与sun jdk有什么关系和区别呢? 历史上的 ...
- 第一百九十四天 how can I坚持
该挺妈妈话的,不该买可乐,该熬点粥喝,肚子疼,救我. 好像每天都一样,每天都在重复.. 哎.. 对了,买了点花种子,铜钱草,牡丹.玫瑰.还买了棵小多肉. 还有,老妈把咸菜给邮过来了,有点期待啊. 连续 ...