题目连接: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酱。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 #include <cctype>
 6 #include <algorithm>
 7 #include <numeric>
 8 #include <map>
 9 #include <vector>
10 using namespace std;
11
12 const int N = 250 * 250;
13 map<int, int> check;
14 vector<int> Stack;
15
16 int main () {
17     int T, n, p, q, cur = 0;
18     int s1[N], s2[N];
19     scanf("%d", &T);
20     while (T --) {
21         Stack.clear();
22         check.clear();
23         scanf("%d%d%d", &n, &p, &q);
24         for (int i = 0; i <= p; ++ i) {
25             scanf("%d", &s1[i]);
26             check[s1[i] ] = i + 1;
27         }
28
29         map<int, int> :: iterator it;
30
31         for (int i = 0; i <= q; ++ i) {
32             scanf("%d", &s2[i]);
33             if (check.find(s2[i]) != check.end()) {
34                 s2[i] = check[s2[i]];
35             } else {
36                 s2[i] = 0;
37             }
38         }
39         /*
40         for (int i = 0 ; i <= p; ++ i) {
41             cout << check[s1[i] ] << " ";
42         }
43         cout << endl;
44         *//*
45         for (int i = 0 ; i <= q; ++ i) {
46             cout << s2[i] << " ";
47         }
48         cout << endl;
49 */
50         for (int i = 0; i <= q; ++ i) {
51             if (Stack.empty() || s2[i] > Stack[Stack.size() - 1]) {
52                 Stack.push_back(s2[i]);
53             } else {
54                 vector<int> :: iterator it = lower_bound(Stack.begin(), Stack.end(), s2[i]);
55                 *it = s2[i];
56             }
57         }
58         /*for (int i =  0; i < Stack.size(); ++ i ) {
59             cout << Stack[i] << " ";
60         }
61         cout << endl;*/
62         printf("Case %d: ", ++ cur);
63         cout << Stack.size() << endl;
64
65     }
66     return 0;
67 }

转载于:https://www.cnblogs.com/Destiny-Gem/p/3865275.html

UVa10653.Prince and Princess相关推荐

  1. HDU 4685 Prince and Princess(二分匹配加点建图+强连通分量)

    题目链接 Problem Description There are n princes and m princesses. Princess can marry any prince. But pr ...

  2. H - Prince and Princess 计蒜客 - 42402

    H - Prince and Princess 计蒜客 - 42402 题意: 你现在要寻找公主,有三种人,第一种是说真话的人(至少为1,因为公主是说真话的人),第二种人是只会说假话的,第三种是胡说八 ...

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

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

  4. uva10635 Prince and Princess LCS 变 lIS

    // uva10635 Prince and Princess LCS 变 lIS // 本意求LCS,但是规模有60000多,复杂度肯定不够 // 注意如果俩个序列的值的范围相同,那么可以在一个 / ...

  5. Prince and Princess问题解决

    摘要 这是突然看到的一个非常有意思的题,是ACM-ICPC的原题,在这里稍微做一些分析,希望对大家有帮助. Prince and Princess 题面 王子m和公主Hff相爱,想要娶公主.虽然公主非 ...

  6. HDU - 4685 Prince and Princess(强连通缩点+二分图完备匹配)

    题目链接:点击查看 题目大意:给出n个王子和m个公主,每个王子都有喜欢的公主,题目需要我们在尽可能多的王子可以匹配到喜欢的公主的情况下,求出每个王子所能娶的所有公主,必须保证王子娶了其中任何一个之后, ...

  7. UVA 10635——Prince and Princess

    题意:给定两个长度为p+1和q+1的序列,求两个序列的LCS. 思路:如果直接使用朴素的LCS算法则O(pq)会超时,可以把A中出现的元素编码,然后映射到B(只保留AB都存在的元素),这样就转化为求B ...

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

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

  9. UVa 10635 (LIS+二分) Prince and Princess

    题目的本意是求LCS,但由于每个序列的元素各不相同,所以将A序列重新编号{1,2,,,p+1},将B序列重新编号,分别为B中的元素在A中对应出现的位置(没有的话就是0). 在样例中就是A = {1 7 ...

  10. Prince and Princess HDU - 4685

    题意: 有 n 个王子和 m 个公主,每个王子有 kik_iki​ 个喜欢的公主,每个公主都喜欢所有的王子,询问每个王子可能会和哪些公主匹配. 题解: 首先 n 个王子 和 m 个公主做一次二分匹配, ...

最新文章

  1. android中DatePickerTimePicker的应用
  2. elasticsearch 索引搜索和索引性能优化配置——思路:去掉不必要的数据,减小数据的磁盘空间占用,同时提升性能...
  3. [云炬创业基础笔记]第五章创业计划评估16
  4. leetcode43. 字符串相乘 经典大数+和*
  5. 多处理机的进程调度方式
  6. 手把手教Jsp上传文件(FileUpload+Servlet)
  7. HDU 2503 a/b + c/d(最大公约数与最小公倍数,板子题)
  8. 从零开始——基于角色的权限管理01(补充)
  9. Hadoop完全分布式 小bug -no.1 为啥我的集群只显示一个datanode!
  10. 数据通信与计算机网络有哪些协议,​数据传输协议都有哪些?五种常用网络协议...
  11. 10分钟搭建树莓派NAS私有云和KODI影音播放系统 (2)---软硬搭配干活不累
  12. 网页设计与制作期末大作业报告——动画家宫崎骏
  13. java contains忽略大小写_关于java:字符串包含-忽略大小写
  14. 给IBM的黑科技跪了:量子计算机强势来袭!
  15. 【OpenCV学习笔记】之离散傅里叶变换(DFT)
  16. 腾讯云通信WebIM事件回调的坑~
  17. android 黑色透明背景,解决Android png透明图片转jpg时背景变黑的问题
  18. 51单片机实现电机控制和LCD显示
  19. Servlet+JSP一文完结
  20. altium designer利用向导画封装库详解

热门文章

  1. 计算机主机mac地址怎么查,电脑mac地址怎么查看【图文】
  2. Android 百度语音合成手把手教学
  3. 2021年春季 PAT乙级(复盘)
  4. 手机如何测光照度_手机摄影,如何进行准确的测光?一篇文章教会你玩转“测光”...
  5. 电子邮件群发软件哪种好 电子邮件群发软件怎么用
  6. Site App轻松创建移动开发
  7. 测试初中英语词汇量软件,背单词最好的软件排名【浅析初中英语单词拼写】
  8. 华为交换机密码遗失怎么办?华为交换机密码恢复方法
  9. 影视处理计算机配置,影视后期制作电脑配置需要什么
  10. 用Qt实现QQ好友列表界面伸缩功能(完全一模一样)(伸展和收缩、抽屉效果、类似树形控件)(鼠标划过QSS效果)