只要求一遍最长不上升和最长不下降就行了
#include <cstdio>
#include <algorithm>
#define MAXN 110
using namespace std;
int n;
int a[MAXN], f[MAXN], g[MAXN];
int ans;
int read() {
int f = 1, k = 0;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') {
f = -1;
}
c = getchar();
}
while (c >= '0' && c <= '9') {
k = k * 10 + c - '0';
c = getchar();
}
return f * k;
}
int main() {
n = read();
for(int i = 1; i <= n; i ++) {
a[i] = read();
f[i] = g[i] = 1;
}
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= i - 1; j ++) {
if(a[i] > a[j]) {
f[i] = max(f[i], f[j] + 1);
}
}
}
for(int i = n; i >= 1; i --) {
for(int j = n; j >= i + 1; j -- ) {
if(a[i] > a[j]) {
g[i] = max(g[i], g[j] + 1);
}
}
}
for(int i = 1; i <= n; i ++) {
ans = max(ans, f[i] + g[i] - 1);
}
printf("%d", n - ans);
return 0;
}