最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)
$ 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: $
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define ll long long
#define db double
#define inf 0x7fffffff
#define rg register int
using namespace std;
int t,n,m,ans,tt;
int a[505];
int b[505];
int f[505][505];
int s[505][505];
inline int qr(){
register char ch; register bool sign=0; rg res=0;
while(!isdigit(ch=getchar())) if(ch=='-')sign=1;
while(isdigit(ch)) res=res*10+(ch^48),ch=getchar();
return sign?-res:res;
}
inline void print(int i,int j){
if(i)for(;i;--i)if(s[i][j]!=-1)
print(i-1,s[i][j]),printf("%d ",b[j]);
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
t=qr();
while(t--){
n=qr(); for(rg i=1;i<=n;++i) a[i]=qr();
m=qr(); for(rg i=1;i<=m;++i) b[i]=qr();
for(rg i=1;i<=n;++i){ rg v=0;
for(rg j=1;j<=m;++j){ s[i][j]=-1;
if(a[i]==b[j])f[i][j]=f[i-1][v]+1,s[i][j]=v;
else f[i][j]=f[i-1][j];
if(b[j]<a[i]&&f[i-1][j]>f[i-1][v])v=j;
}
} ans=0;
for(rg i=1;i<=m;++i) if(f[n][i]>f[n][ans])ans=i;
printf("%d\n",f[n][ans]);
if(f[n][ans]){print(n,ans); puts("");}
}
return 0;
}
最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)的更多相关文章
- POJ 2127 Greatest Common Increasing Subsequence
You are given two sequences of integer numbers. Write a program to determine their common increasing ...
- POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...
- 【简单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 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...
- 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 ...
- LCIS POJ 2172 Greatest Common Increasing Subsequence
题目传送门 题意:LCIS(Longest Common Increasing Subsequence) 最长公共上升子序列 分析:a[i] != b[j]: dp[i][j] = dp[i-1][j ...
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
随机推荐
- shell按日期自动切割nginx日志脚本
shell按日期自动切割nginx日志脚本 参考:自学it网 http://www.zixue.it/. #nginx日志切割脚本 #author:ce #!/bin/bash #设置日志文件存放目录 ...
- substring用法
字符串截取,substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串. substring(int beginIndex, int endIndex) 返回一 ...
- Codeforces 849B Tell Your World (计算几何)
题目链接 Tell Your World 题意 给出N个点(i, xi),问是否存在两条平行的直线,使得每一个点恰好在两条直线的其中一条上. 每条直线必须穿过至少一个点. 考虑每个点和第1个点的斜率, ...
- 【WEB基础】HTML & CSS 基础入门(6)超链接
超链接--文字链接 超链接[hyperlink]是网页中最为常见的元素之一,我们几乎可以在所有的网站页面中找到超链接.每个网站都不止一个页面,这些页面就是利用超链接进行串接.超链接帮我们实现了网页与网 ...
- Windows下使用Nexus搭建Maven私服(安装)
一.下载Nexus 下载OSS最新版:https://www.sonatype.com/download-oss-sonatype 老版本:https://support.sonatype.com/h ...
- iOS Application Security
文章分A,B,C,D 4个部分. A) iOS Application Security 下面介绍iOS应用安全,如何分析和动态修改app. 1)iOS Application security Pa ...
- 【Mybatis】 Mybatis在xml文件中处理大于号小于号的方法【问题】
处理大于小于号的方法: https://www.cnblogs.com/winner-0715/p/6132755.html 第一种方法:用转义字符把">"和"&l ...
- .net core mvc启动顺序以及主要部件2
原文:.net core mvc启动顺序以及主要部件2 前一篇提到WebHost.CreateDefaultBuilder(args)方法创建了WebHostBuilder实例,WebHostBuil ...
- 统计显著性(Statistical significance)
显著性,又称统计显著性(Statistical significance), 是指零假设为真的情况下拒绝零假设所要承担的风险水平,又叫概率水平,或者显著水平. [1] 显著性的含义是指两个群体的态度之 ...
- 第二讲_图像数据处理Image Data Processing
第二讲_图像数据处理Image Data Processing 深度模型出现后被弱化,但是思想的影子在深度模型中可以看到的 图片存储原理 RGB颜色空间:三通道(b,g,r),加法混色 CMY(K): ...