HDU1423:Greatest Common Increasing Subsequence
浅谈\(DP\):https://www.cnblogs.com/AKMer/p/10437525.html
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1423
最长上升子序列:https://www.cnblogs.com/AKMer/p/10437536.html
最长公共子序列:https://www.cnblogs.com/AKMer/p/10437542.html
由于此题求的是最长公共上升子序列,所以我们需要在最长公共子序列的\(n^2dp\)状态再加一个关于权值的维度。
设\(f[i][j][k]\)表示\(a\)的\([1,i]\)与\(b\)的\([1,j]\)的以权值\(k\)结束的最长公共上升子序列是多少。
若\(a[i]!=b[j]\),\(f[i][j][k]=max(f[i-1][j][k],f[i][j-1][k])\)
若\(a[i]==b[j]\),\(f[i][j][k]=max(f[i-1][j][k],f[i][j-1][k],f[i-1][j-1][x]+1)(0\leqslant x<k)\)
时间复杂度:\(O(n^3)\)
空间复杂度:\(O(n^3)\)
但是出题人似乎并不会给一个\(G\)让我们这样搞,所以这种做法因为空间复杂度不过关而以失败告终。
所以我们可以试着消去第三维,把第三维和第二维结合在一起。
令\(f[i][j]\)表示\(a\)序列的区间\([1,i]\)与\(b\)序列的区间\([1,j]\)的以\(b[j]\)结尾的最长公共上升子序列。
若\(a[i]!=b[j]\),\(f[i][j]=f[i-1][j]\)
若\(a[i]==b[j]\),\(f[i][j]=f[i-1][k]+1(0\leqslant k <j,b[k]<b[j])\)
时间复杂度:\(O(n^3)\)
空间复杂度:\(O(n^2)\)
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=505;
int n,m;
int f[maxn][maxn];
int a[maxn],b[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
int main() {
int T=read();
while(T--) {
memset(f,0,sizeof(f));
n=read();
for(int i=1;i<=n;i++)
a[i]=read();
m=read();
for(int i=1;i<=m;i++)
b[i]=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(a[i]!=b[j])f[i][j]=f[i-1][j];
else {
for(int k=0;k<j;k++)
if(b[k]<b[j])f[i][j]=max(f[i-1][k]+1,f[i][j]);
}
int ans=0;
for(int i=1;i<=m;i++)
ans=max(ans,f[n][i]);
printf("%d\n",ans);
if(T)puts("");
}
return 0;
}
其实当\(a[i]==b[j]\)的时候我们要枚举的\(k\)可以在之前的计算中用一个变量\(mx\)存下来,然后\(O(1)\)更新。
时间复杂度:\(O(n^2)\)
空间复杂度:\(O(n^2)\)
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=505;
int n,m;
int f[maxn][maxn];
int a[maxn],b[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
int main() {
int T=read();
while(T--) {
memset(f,0,sizeof(f));
n=read();
for(int i=1;i<=n;i++)
a[i]=read();
m=read();
for(int i=1;i<=m;i++)
b[i]=read();
for(int i=1;i<=n;i++) {
int mx=0;
for(int j=1;j<=m;j++)
if(a[i]!=b[j]) {
f[i][j]=f[i-1][j];
if(b[j]<a[i])mx=max(mx,f[i-1][j]);
}
else f[i][j]=mx+1;
}
int ans=0;
for(int i=1;i<=m;i++)
ans=max(ans,f[n][i]);
printf("%d\n",ans);
if(T)puts("");
}
return 0;
}
HDU1423:Greatest Common Increasing Subsequence的更多相关文章
- 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 ...
- POJ 2127 Greatest Common Increasing Subsequence
You are given two sequences of integer numbers. Write a program to determine their common increasing ...
- HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- 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 ...
随机推荐
- 大数据架构之:Kafka
Kafka 是一个高吞吐.分布式.基于发布订阅的消息系统,利用Kafka技术可在廉价PC Server上搭建起大规模消息系统.Kafka具有消息持久化.高吞吐.分布式.多客户端支持.实时等特性,适用于 ...
- qt_hal_verion
/opt/EmbedSky/B2/linux-3.0.35/drivers/mxc/gpu-viv/hal/kernel/inc/gc_hal_version.h 文件中的具体版本 export DI ...
- 转载:ensemble计划和数据库
原文来源:x2yline在生信进化树上的评论,http://www.biotrainee.com/thread-626-1-1.html Ensemble( ensembl.org网站是常用真核生物参 ...
- 淘宝分类常见---部分显示和全部显示的js效果
需求就是,点击“更多按钮”,显示全部的分类详情,再次点击,显示部分分类. 展开: 收起: 结构: <div class="SubBox" id="SubBox&qu ...
- mybatis 一对多和多对一
在学习MyBatis3的过程中,文档上面一直在强调一个id的东西!在做这个实验的时候,也因为没有理解清楚id含义而导致一对多的“多”中也只有一条数据.id和result的唯一不同是id表示的结果将 ...
- linux平台及windows平台mysql重启方法
各个平台mysql 重启: inux平台及windows平台mysql重启方法 Linux下重启MySQL的正确方法: 1.通过rpm包安装的MySQL service mysqld restart ...
- Mfc 建立窗口线程
之前一直都是在学习C,但是没用MFC写过东西.所以这个算是MFC的一个处女作把. 今天硬着头皮写了个爆破工具,但是界面(edit控制)在显示的时候一下就被卡住了. 于是到处问人,后来有个不错的朋友帮我 ...
- Java -- JDBC 数据库连接池
1. 原理代码示例 public class JdbcPool implements DataSource { private static LinkedList<Connection> ...
- scrapy的操作
- Java执行过程
Java的运行原理 在Java中引入了虚拟机的概念,即在机器和编译程序之间加入了一层抽象的虚拟的机器.这台虚拟的机器在任何平台上都提供给编译程序一个的共同的接口.编译程序只需要面向虚拟机,生成虚拟机能 ...