Files
python/TangDou/AcWing/LIS/1014_3.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

30 lines
663 B
C++

#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n;
int a[N];
int f[N][2];
int main() {
// input
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
// dp
for (int i = 1; i <= n; i++) {
f[i][0] = f[i][1] = 1;
for (int j = 1; j < i; j++) {
if (a[j] < a[i]) f[i][0] = max(f[i][0], f[j][0] + 1);
if (a[j] > a[i]) f[i][1] = max(f[i][1], max(f[j][0], f[j][1]) + 1);
}
}
// find result from all final states
int res = 0;
for (int i = 1; i <= n; i++) res = max(res, max(f[i][0], f[i][1]));
cout << res << endl;
return 0;
}