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.

30 lines
664 B

#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, f[i][0], f[i][1]});
cout << n - res << endl;
return 0;
}