acwing 895 最长上升公共子序列 (线性DP)

题面

在这里插入图片描述

题解

在这里插入图片描述

代码

#include<bits/stdc++.h>

using namespace std;
const int N = 1e3 + 10;

int n;
int a[N];
int f[N];

int main() {
    
    


    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    cin >> n;

    for (int i = 1; i <= n; i++) cin >> a[i];

    for (int i = 1; i <= n; i++) {
    
    
        f[i] = 1;   //初始以a[i]结尾的上升子序列,开始只有自己
        for (int j = 1; j < i; j++) {
    
    
            if (a[j] < a[i]) {
    
       //枚举前面最后一个a[j]小于a[i]就可以更新
                f[i] = max(f[i], f[j] + 1);
            }
        }
    }

    int res = 0;
    for (int i = 1; i <= n; i++) res = max(res, f[i]);

    cout << res << endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_44791484/article/details/114748868