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 ...
随机推荐
- xml转换为对象 微信接口
public sealed class XMLSerilizable { /// <summary> /// XML转换为对象 /// </summary> /// <t ...
- 【Tech】CAS多机部署Server和Java Client端
昨天尝试把cas的java client端部署到另外一台机器,结果就有问题了.(localhost部署cas server和java client端参见:http://www.cnblogs.com/ ...
- css li 间隙
如果 li 未浮动,而 li 子元素浮动,则ie6和ie7下会出现间隙,解决办法是给 li 写上css hack *vertical-align:bottom;
- mini2440移植uboot 2014.04(五)
代码上传到github上:https://github.com/qiaoyuguo/u-boot-2014.04-mini2440 前几篇博文: <mini2440移植uboot 2014.04 ...
- OC_id类型
博客正式开通啦!以后会每天为大家更新知识,将过去学习的笔记发布出来.供大家学习交流. 在Objective-C 中,id 类型是一个独特的数据类型.在概念上,类似Java 的Object 类,可以转 ...
- Go 外部排序-网络版
目录结果 main.go package main import ( "NetworkSort/pipeline" "fmt" "os" & ...
- iptables DNAT、SNAT和MASQUERATE
MASQUERADE 地址伪装,和SNAT功能一样,只不过SNAT使用固定IP地址,MASQUERADE使用网卡上的地址. SNAT配置: iptables -t nat -A POSTROUTING ...
- 通过join方法顺序执行多个线程
方法一:直接用多线程之间的通讯去解决 package com.toov5.test; import javax.imageio.ImageTypeSpecifier; class Res1{ char ...
- mysql一次运行多个SQL文件
在文件 batch.sql 中写下多个SQL文件 source file1.SQLsource file2.SQLsource file3.SQL 然后运行 source batch.sql
- eclipse和myeclipse的配置(基于工作空间)
eclipse和myeclipse的配置是基于工作空间的,一旦工作空间发生改变,就需要重新配置. 以eclipse为例,新建工作空间后,选择Window--->Preferences: 1.在W ...