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

13935381 10635 Prince and Princess Accepted C++ 0.095 2014-07-24 03:41:18

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


解题思路:由于跪在校赛的LCS nlogn算法,所以回来恶补,特意找了一道相同意思的题目。如同此题,简单的LCS类型,但是10^5左右的数据量,如果开滚动dp的话,复杂度O(n^2)会超时。所以应该将LCS问题转化成下标对应的LIS问题,然后再使用LIS的nlogn算法,具体思想是维护一个递增序列,二分找上界,替换元素即可。(感谢DIM神给我讲解此算法)。这样已经足够了。但是说句与此题无关的话,如果数据范围过大,例如到64位级别的数字时,可以使用离散化,继续降低复杂度。膜拜下yeahpeng酱。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <numeric>
#include <map>
#include <vector>
using namespace std; const int N = * ;
map<int, int> check;
vector<int> Stack; int main () {
int T, n, p, q, cur = ;
int s1[N], s2[N];
scanf("%d", &T);
while (T --) {
Stack.clear();
check.clear();
scanf("%d%d%d", &n, &p, &q);
for (int i = ; i <= p; ++ i) {
scanf("%d", &s1[i]);
check[s1[i] ] = i + ;
} map<int, int> :: iterator it; for (int i = ; i <= q; ++ i) {
scanf("%d", &s2[i]);
if (check.find(s2[i]) != check.end()) {
s2[i] = check[s2[i]];
} else {
s2[i] = ;
}
}
/*
for (int i = 0 ; i <= p; ++ i) {
cout << check[s1[i] ] << " ";
}
cout << endl;
*//*
for (int i = 0 ; i <= q; ++ i) {
cout << s2[i] << " ";
}
cout << endl;
*/
for (int i = ; i <= q; ++ i) {
if (Stack.empty() || s2[i] > Stack[Stack.size() - ]) {
Stack.push_back(s2[i]);
} else {
vector<int> :: iterator it = lower_bound(Stack.begin(), Stack.end(), s2[i]);
*it = s2[i];
}
}
/*for (int i = 0; i < Stack.size(); ++ i ) {
cout << Stack[i] << " ";
}
cout << endl;*/
printf("Case %d: ", ++ cur);
cout << Stack.size() << endl; }
return ;
}
												

UVa10653.Prince and Princess的更多相关文章

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

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

  2. 10635 - Prince and Princess

    Problem D Prince and Princess Input: Standard Input Output: Standard Output Time Limit: 3 Seconds In ...

  3. uva 10635 - Prince and Princess(LCS)

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

  4. Prince and Princess HDU - 4685(匹配 + 强连通)

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

  5. UVA - 10635 Prince and Princess LCS转LIS

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

  6. HDU 4685 Prince and Princess 二分图匹配+tarjan

    Prince and Princess 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4685 Description There are n pri ...

  7. HDU 4685 Prince and Princess (2013多校8 1010题 二分匹配+强连通)

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

  8. HDU4685:Prince and Princess(二分图匹配+tarjan)

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

  9. Prince and princess——需要优化的DP

    一个时间效率为o(nlogn)的算法求公共子序列的应用 Prince and princess 题目大意(已翻译 ) 在nxn的棋盘上,王子和公主玩游戏.棋盘上的正方形编号为1.2.3 ... n * ...

随机推荐

  1. Entify Framewrok - Join的使用方法

    问题:有2个表,使用id相连,如何用Join语法将其连接起来? 如下代码 List<tblAssociation> assoList = dataContext.tblAssociatio ...

  2. javascript:DOM自定义属性的妙用

    虽然HTML元素的属性已经十分丰富,但在某些场合下,元素固有的属性无法完成我们的需求: 这个时候,自定义属性就会让问题解决起来比较方便. 比如,下面的栗子: 多张图片点击变化事件:当背景图片为a.jp ...

  3. BoneCP学习笔记

    什么是BoneCP BoneCP 是一个快速.免费而且开源的java数据库连接池(JDBC Pool)管理工具库.如果你曾经使用过C3P0或者DBCP,那你肯定知道上面这句话的意思:如果你没用过这些, ...

  4. 软件工程师所需掌握的“终极技术”是什么?

    软件工程师所需掌握的"终极技术"是什么? http://yunli.blog.51cto.com/831344/1019990 最近,我在微博上看到@程序员邹欣老师发的一条微博 - ...

  5. 定义block块

    一: 工程图 二: 代码区 AppDelegate.h #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <U ...

  6. web开发常用样式

    1.div保持底部浮动(不受滚动条影响) position:fixed;_position:absolute;bottom:0px;_bottom:0px;_margin-top:expression ...

  7. Mysql常用命令记录

    -- 导出数据库: mysqldump -u user_name -p database_name > import_file.sql -- 执行脚本 source database.sql

  8. SQL 经典练习

    SQL 基础练习 CREATE TABLE STUDENT(SNO VARCHAR(3) NOT NULL, SNAME VARCHAR(4) NOT NULL,SSEX VARCHAR(2) NOT ...

  9. C++中的cout输出机制

    代码: #include <iostream> using namespace std; int hello(){ cout<<"hello"<< ...

  10. CDialog类中OnCancel()、OnInitDialog()作用

    1.void CCOMDDlg::OnCancel() { // TODO: Add extra cleanup here CDialog::OnCancel();} 如果把CDialog::OnCa ...