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 ...
随机推荐
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): 问题解决方法
在用maven配置mybatis环境时出现此BindingExceptiony异常,发现在classes文件下没有mapper配置文件,应该是maven项目没有扫描到mapper包下的xml文件,在p ...
- 关于安装php时 --with-mysql命令参数问题
如果是rpm安装mysql则直接写成--with-mysql 如果是编译安装mysql则写成--with-mysql=mysql安装路劲 如果你还没有安装Mysql数据库,可以暂时不编译
- [.NET开发] C#实现剪切板功能
C#剪切板 Clipboard类 我们现在先来看一下官方文档的介绍 位于:System.Windows.Forms 命名空间下 Provides methods to place data on an ...
- English trip -- VC(情景课)3 B Bamily members
xu言: 今天,好困啊 -__-. . zZ 早点睡吧...适当的休息,才能更好的学习 Vocabulary focus husband wife uncle aunt brother sister ...
- LeetCode--168--Excel表列名称
问题描述: 给定一个正整数,返回它在 Excel 表中相对应的列名称. 例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 ...
- spring中@Scope作用域的注解
@Scope简单点说就是用来指定bean的作用域 官方解释是:scope用来声明IOC容器中的对象应该处的限定场景或者说该对象的存活空间,即在IOC容器在对象进入相应的scope之前,生成并装配这些对 ...
- POJ 2263 最短路Floyd_warshall算法
灰常开心的用Floyd变形写出来了.额.对米来说还是牺牲了一定的脑细胞的.然而.我发现.大牛们还可以神奇的用Kruskal求最大生成树的最小权值来写.也可以用Dijkatra变形来写.T_T....5 ...
- Python 数据类型--集合(set)
一.集合(set) 集合也是一种数据类型,一个类似列表的,无序的,不重复的.它主要有两大作用 1.把一个列表变为集合,就自动去重了,不需要写额外的代码 2.关系测试,测试两组数据之间的交际.差集.并集 ...
- Delphi中的文件扩展名
Filename Extensions in Delphi http://delphi.about.com/od/beginners/a/aa032800a.htm Try building a sm ...
- sql server的远程连接
当一台服务器上的数据库需要用到另一台服务器上的数据库时,就需要远程连接 首先创建远程连接 exec sp_addlinkedserver linkname,'','SQLOLEDB',serverIP ...