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 ...
随机推荐
- Swing 添加超链接 打开页面
http://lazycat774880994.iteye.com/blog/567412 Swing中打开一个连接或者web页面的一些记录,这几种方式是在项目中有这样子用到过,特来记录一下,以便下 ...
- nodejs数据接收body-parser中间件
给大家翻译一下npm上body-parser的资料 nodejs 的body数据解析中间件 插件作用:对于req.body属性,在操作数据前分析进来的请求体的插件 首先学习解析一个http处理 这篇文 ...
- nodejs 各种插件
__dirname:全局变量,存储的是文件所在的文件目录 __filename:全局变量,存储的是文件名 代码:dirname.jsconsole.log(__dirname); 运行node dir ...
- 20145239《网络对抗》- 逆向及Bof基础实践
1 逆向及Bof基础实践说明 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件.该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串.该程序同 ...
- Java 访问修饰符总结
Java中的访问修饰符 Java面向对象的基本思想之一是封装细节并且公开接口. Java语言采用访问控制修饰符来封装类及类的方法和属性的访问权限,从而向使用者暴露接口.隐藏细节. Java访问控制分为 ...
- ubuntu 查看系统版本
在终端中执行下列指令:cat /etc/issue可以查看当前正在运行的 Ubuntu 的版本号: 使用 lsb_release 命令也可以查看 Ubuntu 的版本号,与方法一相比,内容更为详细:
- 【bzoj5085】最大(二分+乱搞)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=5085 这道题我们可以先二分答案,然后转化为判定是否有四角权值>=mid的矩形. ...
- this关键字详解
在java中,编译器会为每个对象分配一个this关键字.在代码中使用关键字可以使代码更优雅.下面我就列举一下this关键字常见的几种场景. 1.this代表当前对象调用成员变量和方法,也是用的最多的地 ...
- nova Scheduling 配置
Nova中调度配置: scheduler_driver_task_period = scheduler_driver = nova.scheduler.filter_scheduler.FilterS ...
- Spring Boot入门——邮件发送
1.引入依赖 <!-- mail依赖 --> <dependency> <groupId>org.springframework.boot</groupId& ...