Files
python/C++专题课程/动态规划/动态规划-4-最长上升子序列.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

29 lines
792 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<bits/stdc++.h>
using namespace std;
/*
知识点内容:最长上升子序列
文档内容参考:
https://www.cnblogs.com/aiguona/p/7278141.html
*/
int main() {
int n, i, j, num, h[1000], max[1000];
while (~scanf("%d", &n)) {
num = 1;
for (i = 0; i < n; ++i) {
scanf("%d", &h[i]);
max[i] = 1;
}
for (i = 1; i < n; ++i)/*求最长上升子序列*/
for (j = 0; j < i; ++j) {
/*用两个数组,方便理解,如果只是求最长上升子序列,可以直接max[j]和num比较取较大值*/
if (h[j] < h[i] && max[j] + 1 > max[i])
max[i] = max[j] + 1;
if (num < max[i])
num = max[i];
}
printf("%d\n", num);
}
return 0;
}