✅ 最长下降(上升)子序列模版
This commit is contained in:
parent
071dbbe56d
commit
c648a9cabb
28
longest-decreasing-subsequence/main.cc
Normal file
28
longest-decreasing-subsequence/main.cc
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
#define MAXN 10005
|
||||||
|
#define M 200
|
||||||
|
int dp[MAXN];
|
||||||
|
int arr[M];
|
||||||
|
int longest_decreasing_subsequence(int n) {
|
||||||
|
int maximun = 0;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
dp[i]++;
|
||||||
|
for (int j = 0; j < i; j++) {
|
||||||
|
if (arr[j] > arr[i]) // 最大上升为 `arr[j] < arr[i]`
|
||||||
|
dp[i] = max(dp[i], dp[j] + 1);
|
||||||
|
}
|
||||||
|
if (dp[i] > maximun)
|
||||||
|
maximun = dp[i];
|
||||||
|
}
|
||||||
|
return maximun;
|
||||||
|
}
|
||||||
|
int main() {
|
||||||
|
int N;
|
||||||
|
cin >> N;
|
||||||
|
for (int i = 0; i < N; i++) {
|
||||||
|
cin >> arr[i];
|
||||||
|
}
|
||||||
|
int value = longest_decreasing_subsequence(N);
|
||||||
|
cout << value << endl;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user