272. 最长公共上升子序列

题目链接

272. 最长公共上升子序列

熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目。

小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们研究最长公共上升子序列了。

小沐沐说,对于两个数列 AB,如果它们都包含一段位置不一定连续的数,且数值是严格递增的,那么称这一段数是两个数列的公共上升子序列,而所有的公共上升子序列中最长的就是最长公共上升子序列了。

奶牛半懂不懂,小沐沐要你来告诉奶牛什么是最长公共上升子序列。

不过,只要告诉奶牛它的长度就可以了。

数列 AB 的长度均不超过 3000

输入格式

第一行包含一个整数 N,表示数列 AB 的长度。

第二行包含 N 个整数,表示数列 A

第三行包含 N 个整数,表示数列 B

输出格式

输出一个整数,表示最长公共上升子序列的长度。

数据范围

1N3000,序列中的数字均不超过 2311

输入样例:

4
2 2 1 3
2 1 2 3

输出样例:

2

解题思路

dp

  • 状态表示:f[i][j] 表示 a 序列以 i 结尾,b 序列的前 j 个的最长公共上升子序列的长度

  • 状态计算:

    • a[i]=b[j] 时,f[i][j]=max(f[i][j],f[k][j1]+1),其中 k<i,a[k]<a[i]
    • a[i]b[j] 时,f[i][j]=f[i][j1]
      分析:a 序列以 i 结尾,当 a[i]b[j] 时显然有 f[i][j]=f[i][j1],否则为了满足上升的要求,需要寻找 i前面且 a[i]>a[k]k,即 f[i][j]=max(f[i][j],f[k][j1]+1)
  • 时间复杂度:(n3)

  • 暴力代码

// 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;
}

考虑优化
关键在于寻找 f[k][j1] 的最大值,其中 j1 是固定的,可以将 j 放在外层循环,记录每层循环 a[i]<b[j] 时的最大的 mx=f[i][j1]+1,当 a[i]=b[j] 时由于 mxi 之前计算过的,且由于 j 是固定的,所以 mx 满足上升这个性质,然后直接比较即可

  • 时间复杂度:O(n2)

代码

// 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 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(39)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示