浅谈\(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的更多相关文章

  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 ...

  2. Greatest Common Increasing Subsequence hdu1423

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  3. HDU 1423 Greatest Common Increasing Subsequence LCIS

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

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

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

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

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

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

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

  7. POJ 2127 Greatest Common Increasing Subsequence

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

  8. HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  9. 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 ...

随机推荐

  1. 前端之CSS进阶

    一.CSS属性操作 1.背景属性 常用: background-color 规定要使用的背景颜色 background-image 规定要使用的背景图像 background-repeat 规定如何重 ...

  2. LVS/NAT 配置

    LVS/NAT 配置 实验环境 三台主机:Linux Centos 6.4 32位 调度器Director:192.168.1.160(内网IP).192.168.2.20(公网IP) HTTP真实服 ...

  3. https网站无法加载http路径的js和css

    在https的网站中引用http路径的js或css会导致不起作用,其形如: <script src="http://code.jquery.com/jquery-1.11.0.min. ...

  4. jQuery仿Android锁屏图案应用

    在线演示 本地下载

  5. jQuery滑动杆打分插件

    在线演示 本地下载

  6. iOS_Quartz 2D绘图

    目  录: 一.基础知识掌握 二.Quartz 2D绘图基础:CGContextRef实现简单地绘制图形 三.CGContextRef实现文字.图片.基于路径的图形绘制 四.在内存中绘制位图 五.添加 ...

  7. Docker 配置代理

    最近在k8s上部署helm 老提示无法下载镜像,因为伟大的祖国的长城Firewall....导致k8s根本玩不了..... 第一步:配置系统代理 # vim .bashrc export http_p ...

  8. KestrelHttpServer

    source code of Kestrel of documentation https://github.com/aspnet/KestrelHttpServer https://github.c ...

  9. navigationBar

    1.navigationBar导航条可以看做是self.navigationController导航控制器的一个属性. 通过self.navigationController.navigationBa ...

  10. nginx 反向代理配置之---可配置多域名请求

    配置文件如下: server { listen 80; server_name ngin服务器所对应的的域名; error_log /data/logs/nginx/mainsite.error.lo ...