HDU1423 Greatest Common Increasing Subsequence
题意
如标题。
\(|s1|,|s2| \leq 500\)
分析
既然是dp问题的组合,那么考虑dp。
定义状态f(i,j)表示对第一个序列s1的前i个和第二个序列s2的前j个元素求最长上升公共子序列,并且s1的第i个元素和s2的第j个元素匹配的结果,显然,当且仅当s1[i]=s2[j]时,f(i,j)有意义。
转移方程为:
\[f(i,j)=\max\{f(i',j')|i'<i,j'<j,s2[j']<s1[i]\}+1\]
这个朴素做法的时间复杂度为\(O(n^4)\)。我们尝试优化此方程,记\(opt(j')=\max\{f(i',j')\}\),保证f(i',j')有意义。那么转移方程为:
\[f(i,j)=\max\{opt(j')|j'<j,s2[j']<s1[i]\}+1\]
事实上,只需要从小到大枚举i,然后及时更新opt,就可以将转移的复杂度降为O(n)了。这样时间复杂度为\(O(n^3)\),已经可以过了。
代码
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<complex>
#pragma GCC optimize ("O0")
using namespace std;
template<class T> inline T read(T&x)
{
T data=0;
int w=1;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
data=10*data+ch-'0',ch=getchar();
return x=data*w;
}
typedef long long ll;
const int INF=0x7fffffff;
const int MAXN=5e2+7;
int n,m;
int a[MAXN],b[MAXN];
int f[MAXN][MAXN];
int opt[MAXN];
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int T;
read(T);
while(T--)
{
memset(f,0,sizeof(f)); // edit 1
memset(opt,0,sizeof(opt));
read(n);
for(int i=1;i<=n;++i)
read(a[i]);
read(m);
for(int i=1;i<=m;++i)
read(b[i]);
int ans=0;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
{
if(a[i]!=b[j])
continue;
int t=0;
for(int k=1;k<j;++k)
if(b[k]<a[i])
t=max(t,opt[k]);
f[i][j]=t+1;
opt[j]=max(opt[j],f[i][j]);
ans=max(ans,f[i][j]);
}
printf("%d\n",ans);
if(T) // edit 2
puts("");
}
// fclose(stdin);
// fclose(stdout);
return 0;
}
HDU1423 Greatest Common Increasing Subsequence的更多相关文章
- HDU4512完美队形I && HDU1423 Greatest Common Increasing Subsequence (LCIS)
填坑的时候又到啦,校赛因为不会LCIS所以吃了大亏,这里要补起来.LCIS就是在两个串里找最长上升子序列,相关的博客有很多,这里自己就不写那么多了. http://www.cnblogs.com/ja ...
- HDU1423 Greatest Common Increasing Subsequence (DP优化)
LIS和LCS的结合. 容易写出方程,复杂度是nm2,但我们可以去掉一层没有必要的枚举,用一个变量val记录前一阶段的最优解,这样优化成nm. 1<=k<j,j增加1,k的上界也增加1,就 ...
- HDU1423:Greatest Common Increasing Subsequence(LICS)
Problem Description This is a problem from ZOJ 2432.To make it easyer,you just need output the lengt ...
- Greatest Common Increasing Subsequence hdu1423
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
随机推荐
- Filters: before, after, around, 常用到before_action,执行顺序是从外到内。
Filters是继承的,所以可以在ApplicationControlooer中设置filters. 如果有多个过滤,它们执行的顺序 先父类,然后往下推倒到子类 同一个类,按照代码上下顺序执行.
- (转)理解TIME_WAIT,彻底弄清解决TCP: time wait bucket table overflow
转载自http://blog.51cto.com/benpaozhe/1767612: 一直对这个问题知其然而不知其所以然,这些日子再次碰到,看了很多的资料,彻底解决一下,呵呵,先上个图,所有理解围绕 ...
- c++中的new和delete
对于计算机程序设计而言,变量和对象在内存中的分配都是编译器在编译程序时安排好的,这带来了极大的不便,如数组必须大开小用,指针必须指向一个已经存在的变量或对象.对于不能确定需要占用多少内存的情况,动态内 ...
- Wannafly挑战赛20-A,B
A-链接:https://www.nowcoder.com/acm/contest/133/A来源:牛客网 题目描述 现在有一棵被Samsara-Karma染了k种颜色的树,每种颜色有着不同的价值 A ...
- UVA-11584 Partitioning by Palindromes (简单线性DP)
题目大意:给一个全是小写字母的字符串,判断最少可分为几个回文子序列.如:“aaadbccb” 最少能分为 “aaa” “d” “bccb” 共三个回文子序列,又如 “aaa” 最少能分为 1 个回文子 ...
- 基础最短路(模板 bellman_ford)
Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店 ...
- 追加XML
追加有两种情况,这个文档可能存在,也可能不存在 XmlDocument doc = new XmlDocument();XmlElement books; //将books声明在外边这样后面的代码才可 ...
- java使用HttpClient
HttpClient常用的包有两个 org.apache.http.client以及org.apache.commons.httpclient 我常用的是org.apache.http.client. ...
- OC 对象和函数
#import <Foundation/Foundation.h> @interface Car : NSObject {// 成员变量 @public int wheels; int s ...
- win7下安装node及出现的npm问题
按照官网下载安装,选择 Windows Installer (.msi):,一直next安装,默认安装在C:\Program Files\nodejs下,环境变量会自动添加 如果安装后,打开cmd输入 ...