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

Sequence S1, S2, ..., SN of length N is called an increasing subsequence of a sequence A1, A2, ..., AM of length M if there exist 1 <= i1 < i2 < ...< iN <= M such that Sj = Aij for all 1 <= j <= N, and Sj < Sj+1 for all 1 <= j < N.

Input

Each sequence is described with M - its length (1 <= M <= 500) and M integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.

Output

On the first line of the output print L - the length of the greatest common increasing subsequence of both sequences. On the second line print the subsequence itself. If there are several possible answers, output any of them.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Sample Input

1

5
1 4 2 5 -12
4
-12 1 2 4

Sample Output

2
1 4

题意 

输出两个序列的最长公共上升子序列。

分析

初始想法:定义dp[i][j]为以a[i]和b[j]为结尾的LCIS,这样转移时得找ax<ai以及by<bj,需要n^2,加上转移的循环,总复杂度n^4,TLE。

正解:既然上述定义超时,那么我们尝试减少一维,即把dp[i][j]定义为a[1...i]和b[1...j]并以b[j]为结尾的LCIS。

当a[i]==b[j],由LCS的转移可知由dp[i-1][j-1],但由于我们定义的这个状态,转移应为dp[i][j]=max(dp[i][k]),k<j。

当a[i]!=b[j],dp[i][j]=dp[i-1][j],因为规定了以b[j]为结尾,所以此时不可以由dp[i][j-1]转移而来。

另外可以优化一下,因为j是从小到大枚举的,那么我们可以保存当前行最大的dp[i][k]且符合b[k]<a[i](为了某个a[i]==b[x]的转移服务),到需要转移时就可以直接使用了

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include <queue>
#include <vector>
#include<bitset>
#include<map>
#include<deque>
using namespace std;
typedef long long LL;
const int maxn = 1e4+;
const int mod = +;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
//#define mp make_pair
#define ms(a,b) memset(a,b,sizeof(a))
const int inf = 0x3f3f3f3f;
#define lson l,m,2*rt
#define rson m+1,r,2*rt+1
typedef long long ll;
#define N 100010 int a[],b[];
int dp[][],pos[][]; int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int t,n,m;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
scanf("%d",&m);
for(int j=;j<=m;j++) scanf("%d",&b[j]);
ms(dp,); int ans=-,mx,ei=,ej=,mj;
for(int i=;i<=n;i++){
mx=;
for(int j=;j<=m;j++){
dp[i][j]=dp[i-][j];
pos[i][j]=-;
if(b[j]<a[i]&&dp[i-][j]>mx){
mx=dp[i-][j];
mj=j;
}else if(a[i]==b[j]){
dp[i][j]=mx+;
pos[i][j]=mj;
}
if(ans<dp[i][j]){
ans=dp[i][j];
ei=i;
ej=j;
}
}
}
cout<<ans<<endl;
int temp[];
int tmp=ans;
while(ans){
if(pos[ei][ej]!=-){
temp[ans--]=b[ej];
ej=pos[ei][ej];
}
ei--;
}
for(int i=;i<=tmp;i++){
printf("%d%c",temp[i],i==tmp?'\n':' ');
}
if(t) puts("");
}
return ;
}

POJ 2127 Greatest Common Increasing Subsequence的更多相关文章

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

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

  2. 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)

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

  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. HDU 1423 Greatest Common Increasing Subsequence LCIS

    题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  7. HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...

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

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

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

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

随机推荐

  1. JS判断浏览器种类

    function myBrowser() {                        var userAgent = navigator.userAgent; //取得浏览器的userAgent ...

  2. C语言复制文件的两种简单的方法【从根本解决问题】

    网上的方法大致有这样几种: 1.使用操作系统提供的复制文件的API 2.使用C语言本身提供的复制文件的函数 3.直接读写文件,从文件角度来操作,从而直接将一个文件复制 这里我们使用的就是这第三种. 复 ...

  3. Docker安装指定版本

    今天新增一个Docker服务器,Docker安装顺利,启动hello-world测试的时候却出现了问题: $ docker run hello-worldUnable to find image 'h ...

  4. phpStorm字体大小无法调整, 怎么办?

    最近上手了一款轻量级IDE phpStorm,可是就在调整编辑器字体大小时却遇到问题了, 发现字体大小无法调整,另外还有字体大小往左还有个“√”,始终无法去掉,这个勾限制了字体系列,就可怜巴巴的那几个 ...

  5. python 安装influxdb-python

    一.Linux下安装 1.yum install -y git 2.安装pip,参考:https://app.yinxiang.com/shard/s41/sh/0338ba85-5443-453f- ...

  6. 基于C#.NET的高端智能化网络爬虫(一)(反爬虫哥必看)

    前两天朋友发给我了一篇文章,是携程网反爬虫组的技术经理写的,大概讲的是如何用他的超高智商通过(挑衅.怜悯.嘲讽.猥琐)的方式来完美碾压爬虫开发者.今天我就先带大家开发一个最简单低端的爬虫,突破携程网超 ...

  7. ansible系列8-SSH连接和执行性能优化

    1. 当你的SSH的版本高于5.6时 我们可以直接修改 /etc/ansible/ansible.cfg里面的参数 ssh_args = -C -o ControlMaster=auto -o Con ...

  8. BZOJ4010[HNOI2015]菜肴制作——拓扑排序+堆

    题目描述 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜肴预估的质量从高到低给予 1到N的顺序编号,预估质量最高的菜肴编号为1.由于菜肴 ...

  9. 查看本地Git仓库历史修改内容

    查看历史内容 在.git文件 同级目录下,右键 选择 git history 但是红框中的路径无法拷贝.右键红框中的任一文件,有 HighLight this only, Highlight this ...

  10. MT【27】对数方程组求范围

    解答:3 评论:此类题目通性通法为换元后化归为线性规划问题.当然不等式凑配也是常见技巧,只是容易范围扩大或者缩小.