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

在样例中就是A = {1 7 5 4 8 3 9},B = {1 4 3 5 6 2 8 9}

重新编号以后:

A = {1 2 3 4 5 6 7}, B = {1 4 6 3 0 0 5 7}(里面的0在求LIS时可以忽略)

这样求A、B的LCS就转变为求B的LIS

求LIS用二分优化,时间复杂度为O(nlogn)

第一次做的用二分求LIS的题是HDU 1025

http://www.cnblogs.com/AOQNRMGYXLMV/p/3862139.html

在这里再复习一遍

  1. //#define LOCAL
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. const int maxn = * ;
  9. int num[maxn], s[maxn], dp[maxn];
  10.  
  11. int main(void)
  12. {
  13. #ifdef LOCAL
  14. freopen("10635in.txt", "r", stdin);
  15. #endif
  16.  
  17. int T, kase;
  18. scanf("%d", &T);
  19. for(kase = ; kase <= T; ++kase)
  20. {
  21. int N, p, q, x;
  22. scanf("%d%d%d", &N, &p, &q);
  23. memset(num, , sizeof(num));
  24. for(int i = ; i <= p+; ++i)
  25. {
  26. scanf("%d", &x);
  27. num[x] = i;
  28. }
  29. int n = ;
  30. for(int i = ; i <= q+; ++i)
  31. {
  32. scanf("%d", &x);
  33. if(num[x])
  34. s[n++] = num[x];
  35. }
  36. //求s[1]...s[n]的LIS
  37. dp[] = s[];
  38. int len = ;
  39. for(int i = ; i <= n; ++i)
  40. {
  41. int left = , right = len;
  42. while(left <= right)
  43. {
  44. int mid = (left + right) / ;
  45. if(dp[mid] < s[i])
  46. left = mid + ;
  47. else
  48. right = mid - ;
  49. }
  50. dp[left] = s[i];
  51. if(left > len)
  52. ++len;
  53. }
  54.  
  55. printf("Case %d: %d\n", kase, len);
  56. }
  57. return ;
  58. }

代码君

大白书里面用到了lower_bound函数

函数介绍

lower_bound()返回一个 iterator 它指向在[first,last)标记的有序序列中可以插入value,而不会破坏容器顺序的第一个位置,而这个位置标记了一个大于value 的值。

效果是一样的

  1. //#define LOCAL
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. const int INF = ;
  9. const int maxn = * ;
  10. int num[maxn], s[maxn], g[maxn], d[maxn];
  11.  
  12. int main(void)
  13. {
  14. #ifdef LOCAL
  15. freopen("10635in.txt", "r", stdin);
  16. #endif
  17.  
  18. int T, kase;
  19. scanf("%d", &T);
  20. for(kase = ; kase <= T; ++kase)
  21. {
  22. int N, p, q, x;
  23. scanf("%d%d%d", &N, &p, &q);
  24. memset(num, , sizeof(num));
  25. for(int i = ; i <= p+; ++i)
  26. {
  27. scanf("%d", &x);
  28. num[x] = i;
  29. }
  30. int n = ;
  31. for(int i = ; i <= q+; ++i)
  32. {
  33. scanf("%d", &x);
  34. if(num[x])
  35. s[n++] = num[x];
  36. }
  37. //求s[1]...s[n]的LIS
  38. for(int i = ; i <= n; ++i)
  39. g[i] = INF;
  40. int ans = ;
  41. for(int i = ; i < n; ++i)
  42. {
  43. int k = lower_bound(g+, g+n+, s[i]) - g;
  44. d[i] = k;
  45. g[k] = s[i];
  46. ans = max(ans, d[i]);
  47. }
  48. printf("Case %d: %d\n", kase, ans);
  49. }
  50. return ;
  51. }

代码君

UVa 10635 (LIS+二分) Prince and Princess的更多相关文章

  1. UVA - 10635 LIS LCS转换

    白书例题,元素互不相同通过哈希转换为LIS求LCS #include<iostream> #include<algorithm> #include<cstdio> ...

  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—— 求LCS(最长公共子序列)转换成 求LIS(最长递增子序列)

    题目大意:有n*n个方格,王子有一条走法,依次经过m个格子,公主有一种走法,依次经过n个格子(不会重复走),问他们删去一些步数后,重叠步数的最大值. 显然是一个LCS,我一看到就高高兴兴的打了个板子上 ...

  4. uva 10635 - Prince and Princess(LCS)

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

  5. 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 ...

  6. UVA 10653.Prince and Princess

    题目 In an n * n chessboard, Prince and Princess plays a game. The squares in the chessboard are numbe ...

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

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

  8. 10635 - Prince and Princess

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

  9. UVa10653.Prince and Princess

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

随机推荐

  1. 火狐和IE之间的7个JavaScript差异

    尽管 JavaScript 历史上使用冗长而令人生厌的代码块来标的特定浏览器的时期已经结束了,但是偶尔使用一些简单的代码块和对象检测来确保一些代码在用户机器上正常工作依然是必要的. 这篇文章中,我会略 ...

  2. appium获取android app的包名和主Activity

    方法一在appium的android setting中选择下载到电脑上的app包,获取Activity. 方法二在android-sdk中安装build-tools包,进入这个目录.aapt dump ...

  3. C#索引器及示例

    public class IndexSeletor<T> where T:struct { private List<T> _listObj; public IndexSele ...

  4. POJ 2724

    Purifying Machine Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4014   Accepted: 1127 ...

  5. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  6. 2014多校第二场1011 || HDU 4882 ZCC Loves Codefires (贪心)

    题目链接 题意 : 给出n个问题,每个问题有两个参数,一个ei(所要耗费的时间),一个ki(能得到的score).每道问题需要耗费:(当前耗费的时间)*ki,问怎样组合问题的处理顺序可以使得耗费达到最 ...

  7. Understanding node.js

    Node.js has generally caused two reactions in people I've introduced it to. Basically people either ...

  8. JS异步加载的三种方式

    js加载的缺点:加载工具方法没必要阻塞文档,过得js加载会影响页面效率,一旦网速不好,那么整个网站将等待js加载而不进行后续渲染等工作. 有些工具方法需要按需加载,用到再加载,不用不加载,. 默认正常 ...

  9. Quartz 2D Programming Guide

    Quartz 2D Programming  Guide 官方文档: Quartz 2D Programming Guide 译文: Quartz 2D编程指南(1) - 概览 Quartz 2D编程 ...

  10. UBUNTU 14.04 + CUDA 7.5 + CAFFE

    这个也是困扰我很久的问题,之前用 http://www.cnblogs.com/platero/p/3993877.html 的安装方法,装了五六七八九十次,总是出问题. 后来找到了一种新的方法,一个 ...