272. 最长公共上升子序列

题目链接

272. 最长公共上升子序列

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

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

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

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

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

数列 \(A\)\(B\) 的长度均不超过 \(3000\)

输入格式

第一行包含一个整数 \(N\),表示数列 \(A,B\) 的长度。

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

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

输出格式

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

数据范围

\(1≤N≤3000\),序列中的数字均不超过 \(2^{31}−1\)

输入样例:

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

  • 暴力代码

// 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][j-1]\) 的最大值,其中 \(j-1\) 是固定的,可以将 \(j\) 放在外层循环,记录每层循环 \(a[i]<b[j]\) 时的最大的 \(mx=f[i][j-1]+1\),当 \(a[i]=b[j]\) 时由于 \(mx\)\(i\) 之前计算过的,且由于 \(j\) 是固定的,所以 \(mx\) 满足上升这个性质,然后直接比较即可

  • 时间复杂度:\(O(n^2)\)

代码

// 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;
}
posted @ 2022-03-02 14:44  zyy2001  阅读(38)  评论(0编辑  收藏  举报