#include <stdio.h>
#include <stdlib.h>
template<class T>
static inline void checkMax(T& dst, T src) {
if (src > dst) dst = src;
}
static int solve(int* h, int n)
{
int* longest_up;
int* longest_turn;
longest_up = (int*) malloc(sizeof(int) * n);
longest_turn = (int*) malloc(sizeof(int) * n);
longest_up[0] = 1;
for (int i = 1; i < n; i++) {
longest_up[i] = 1;
for (int j = 0; j < i; j++) {
if (h[i] > h[j])
checkMax(longest_up[i], longest_up[j] + 1);
}
}
longest_turn[0] = 1;
for (int i = 1; i < n; i++) {
longest_turn[i] = 1;
for (int j = 0; j < i; j++) {
if (h[i] < h[j]) {
checkMax(longest_turn[i], longest_turn[j] + 1);
checkMax(longest_turn[i], longest_up[j] + 1);
}
}
}
int max_len = 0;
for (int i = 0; i < n; i++) {
checkMax(max_len, longest_turn[i]);
checkMax(max_len, longest_up[i]);
}
free(longest_up);
free(longest_turn);
return n - max_len;
}
int main()
{
int n;
int h[300];
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &h[i]);
printf("%d\n", solve(h, n));
}
|