0
点赞
收藏
分享

微信扫一扫

1281:最长上升子序列 LIS dp

夏侯居坤叶叔尘 2022-05-01 阅读 67

在这里插入图片描述

分析 O (n^2)

参考大佬:最长回文子串 and 最长回文子序列
在这里插入图片描述
在这里插入图片描述

#include "bits/stdc++.h"

using namespace std;
int a[1010];
int dp[1010];//dp[i]表示代表以 a[i] 结尾的 LIS 的长度
int main() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
        dp[i] = 1;//设为本身
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j < i; j++) {//遍历开头到i这段序列
            if (a[j] < a[i]) {
                dp[i] = max(dp[i], dp[j] + 1);//以a[i]结尾的数的最长递增子序列长度为:当前自己本身的长度 和 在a[i]之前的最长上升子序列+1
            }
        }
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        ans = max(ans, dp[i]);
    }
    printf("%d", ans);
    return 0;
}

举报

相关推荐

0 条评论