272. 最长公共上升子序列
题目链接
272. 最长公共上升子序列
熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目。
小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们研究最长公共上升子序列了。
小沐沐说,对于两个数列 和 ,如果它们都包含一段位置不一定连续的数,且数值是严格递增的,那么称这一段数是两个数列的公共上升子序列,而所有的公共上升子序列中最长的就是最长公共上升子序列了。
奶牛半懂不懂,小沐沐要你来告诉奶牛什么是最长公共上升子序列。
不过,只要告诉奶牛它的长度就可以了。
数列 和 的长度均不超过 。
输入格式
第一行包含一个整数 ,表示数列 的长度。
第二行包含 个整数,表示数列 。
第三行包含 个整数,表示数列 。
输出格式
输出一个整数,表示最长公共上升子序列的长度。
数据范围
,序列中的数字均不超过 。
输入样例:
4
2 2 1 3
2 1 2 3
输出样例:
2
解题思路
dp
-
状态表示: 表示 序列以 结尾, 序列的前 个的最长公共上升子序列的长度
-
状态计算:
-
- 时,,其中
-
- 时,
分析: 序列以 结尾,当 时显然有 ,否则为了满足上升的要求,需要寻找 前面且 的 ,即
- 时,
-
时间复杂度:
-
暴力代码
// Problem: 最长公共上升子序列
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/274/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// %%%Skyqwq
#include <bits/stdc++.h>
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
const int N=3005;
int f[N][N],n,a[N],b[N];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>b[i];
a[0]=b[0]=-0x3f3f3f3f;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(a[i]==b[j])
{
for(int k=0;k<i;k++)
if(a[k]<a[i])f[i][j]=max(f[i][j],f[k][j-1]+1);
}
else
f[i][j]=f[i][j-1];
int res=0;
for(int i=1;i<=n;i++)res=max(res,f[i][n]);
cout<<res;
return 0;
}
考虑优化:
关键在于寻找 的最大值,其中 是固定的,可以将 放在外层循环,记录每层循环 时的最大的 ,当 时由于 是 之前计算过的,且由于 是固定的,所以 满足上升这个性质,然后直接比较即可
- 时间复杂度:
代码
// Problem: 最长公共上升子序列
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/274/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// %%%Skyqwq
#include <bits/stdc++.h>
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
template <typename T> void inline read(T &x) {
int f = 1; x = 0; char s = getchar();
while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
const int N=3005;
int f[N][N],n,a[N],b[N];
int main()
{
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>b[i];
for(int j=1;j<=n;j++)
{
int mx=1;
for(int i=1;i<=n;i++)
{
if(a[i]<b[j])mx=max(mx,f[i][j-1]+1);
if(a[i]==b[j])f[i][j]=max(f[i][j],mx);
else
f[i][j]=f[i][j-1];
}
}
int res=0;
for(int i=1;i<=n;i++)res=max(res,f[i][n]);
cout<<res;
return 0;
}
__EOF__

本文作者:acwing_zyy
本文链接:https://www.cnblogs.com/zyyun/p/15955045.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
本文链接:https://www.cnblogs.com/zyyun/p/15955045.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效