![[AcWing 895] 最长上升子序列](https://pic.wangt.cc/download/pic_router.php?path=https://img2022.cnblogs.com/blog/2674359/202205/2674359-20220523175420251-1959470145.png)
复杂度 $ O(n^{2}) $
总体复杂度 $ 1000^{2} = 1 times 10^{6} $
点击查看代码
#include<iostream>
using namespace std;
const int N = 1010;
int n;
int a[N], f[N];
int main()
{
cin >> n;
for (int i = 0; i < n; i ++) cin >> a[i];
for (int i = 0; i < n; i ++) {
f[i] = 1;
for (int j = 0; j < i; j ++) {
if (a[j] < a[i])
f[i] = max(f[i], f[j] + 1);
}
}
int res = 0;
for (int i = 0; i < n; i ++) res = max(res, f[i]);
cout << res << endl;
return 0;
}
- 状态表示
$ f[i] $ 表示以 $ a[i] $ 为结尾的上升子序列的长度
- 状态转移
$ f[i] = max(f[j]) + 1 $ ,其中 $ j = 0, 1, 2, 3, cdots, i - 1 $ ,并且 $ a[j] < a[i] $