$ Greatest Common Increasing Subsequence $

大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案)



$ solution: $

这道题如果不看具体方案,且我们要求的子序列不存在相同的元素,那么我们可以用 $ cdq $ 分治来搞搞,首先我们记录第二个数列中的元素在第一个数列里出现的位置(假设不存在重复,没有的不管),然后我们的第二个数列的每一个元素就有两个权值了,这是我们只需要两个权值均单调递增即可(这是 $ cdq $ 完全可以干的事情)

但是这道题的限制太多了,不过相应的它的数据范围也比较宽松允许 $ n^2 $ ,而众所周知复杂度越高的算法覆盖面越广,而且我们知道我们要求的序列其中每一个元素必然在两个数列中都存在且从前往后排列,所以我们模仿LCS的几种求法中的第一种。其实这一题和它很像,只不过由于“上升”这两个字而是它状态转移发生了变化。

我们由于要“上升”,那我们的转移显然不能只看上一个状态,在枚举到了哪一个 $ A[i] $ 时,我们所有的小于 $ A[i] $ 的 $ B[j] $ 都可以作为待定的“上一个状态”,只要枚举到了一个和 $ A[i] $ 相同的 $ B[j] $ 我们都可以用这个“上一个状态”转移过来也就是:

$ F[i][j]=max{F[i-1][k]_{B[k]<A[i]} }+1 $



$ code: $

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<iomanip>
  4. #include<algorithm>
  5. #include<cstring>
  6. #include<cstdlib>
  7. #include<ctime>
  8. #include<cmath>
  9. #include<vector>
  10. #include<queue>
  11. #include<map>
  12. #include<set>
  13. #define ll long long
  14. #define db double
  15. #define inf 0x7fffffff
  16. #define rg register int
  17. using namespace std;
  18. int t,n,m,ans,tt;
  19. int a[505];
  20. int b[505];
  21. int f[505][505];
  22. int s[505][505];
  23. inline int qr(){
  24. register char ch; register bool sign=0; rg res=0;
  25. while(!isdigit(ch=getchar())) if(ch=='-')sign=1;
  26. while(isdigit(ch)) res=res*10+(ch^48),ch=getchar();
  27. return sign?-res:res;
  28. }
  29. inline void print(int i,int j){
  30. if(i)for(;i;--i)if(s[i][j]!=-1)
  31. print(i-1,s[i][j]),printf("%d ",b[j]);
  32. }
  33. int main(){
  34. //freopen(".in","r",stdin);
  35. //freopen(".out","w",stdout);
  36. t=qr();
  37. while(t--){
  38. n=qr(); for(rg i=1;i<=n;++i) a[i]=qr();
  39. m=qr(); for(rg i=1;i<=m;++i) b[i]=qr();
  40. for(rg i=1;i<=n;++i){ rg v=0;
  41. for(rg j=1;j<=m;++j){ s[i][j]=-1;
  42. if(a[i]==b[j])f[i][j]=f[i-1][v]+1,s[i][j]=v;
  43. else f[i][j]=f[i-1][j];
  44. if(b[j]<a[i]&&f[i-1][j]>f[i-1][v])v=j;
  45. }
  46. } ans=0;
  47. for(rg i=1;i<=m;++i) if(f[n][i]>f[n][ans])ans=i;
  48. printf("%d\n",f[n][ans]);
  49. if(f[n][ans]){print(n,ans); puts("");}
  50. }
  51. return 0;
  52. }

最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)的更多相关文章

  1. POJ 2127 Greatest Common Increasing Subsequence

    You are given two sequences of integer numbers. Write a program to determine their common increasing ...

  2. POJ 2127 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...

  3. 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】

    Sample Input 5 1 4 2 5 -12 4 -12 1 2 4 Sample Output 2 1 4 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...

  4. POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1423 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  5. LCIS POJ 2172 Greatest Common Increasing Subsequence

    题目传送门 题意:LCIS(Longest Common Increasing Subsequence) 最长公共上升子序列 分析:a[i] != b[j]: dp[i][j] = dp[i-1][j ...

  6. ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)

    Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  7. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  8. HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)

    HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

随机推荐

  1. 【bzoj4240】有趣的家庭菜园 贪心+树状数组

    题目描述 对家庭菜园有兴趣的JOI君每年在自家的田地中种植一种叫做IOI草的植物.JOI君的田地沿东西方向被划分为N个区域,由西到东标号为1~N.IOI草一共有N株,每个区域种植着一株.在第i个区域种 ...

  2. BZOJ1923 [Sdoi2010]外星千足虫 【高斯消元】

    题目 输入格式 第一行是两个正整数 N, M. 接下来 M行,按顺序给出 Charles 这M次使用"点足机"的统计结果.每行 包含一个"01"串和一个数字,用 ...

  3. 【C++】DLL内共享数据区在进程间共享数据(重要)

    因项目需要,需要在DLL中共享数据,即DLL中某一变量只执行一次,在运行DLL中其他函数时该变量值不改变:刚开始想法理解错误,搜到了DLL进程间共享数据段,后面发现直接在DLL中定义全局变量就行,当时 ...

  4. 使用selenium抓取淘宝的商品信息

    淘宝的页面大量使用了js加载数据,所以采用selenium来进行爬取更为简单,selenum作为一个测试工具,主要配合无窗口浏览器phantomjs来使用. import re from seleni ...

  5. springmvc中ajax处理

    1.使用HttpServletResponse处理--不需要配置解析器 @Controller public class AjaxController { @RequestMapping(" ...

  6. Mac OS X 下安装python的MySQLdb模块

    参考资料: mac os x下python安装MySQLdb模块   http://www.codeif.com/post/1073/ MAC OSX使用Python安装模块有关问题  http:// ...

  7. Shiro源代码分析之两种Session的方式

    1.Shiro默认的Session处理方式 <!-- 定义 Shiro 主要业务对象 --> <bean id="securityManager" class=& ...

  8. [Testing] Static Analysis Testing JavaScript Applications

    The static code analysis and linting tool ESLint is the de-facto standard for linting JavaScript pro ...

  9. SolidEdge如何在零件上写字 如何绘制文字

    在草图状态下,插入-文字轮廓   可以按这两个按钮调节文字的大小和位置   之后你可以通过长出或除料把文字凸起或者凹下去   如果你要制作路径文字(比如环形文字),则先绘制一条圆或一段圆弧,并设为构造 ...

  10. UP Board USB无线网卡选购指南

    前言 原创文章,转载引用务必注明链接,水平有限,欢迎指正. 本文环境:ubilinux 3.0 kernel 4.4.0 本文使用Markdown写成,为获得更好的阅读体验和正常的图片.链接,请访问我 ...