You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
python/C++专题课程/动态规划/动态规划-4-最长上升子序列.cpp

29 lines
764 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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;
}