1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <cstdio>
#include <algorithm>

const int max_n = 100000;
int n;
int in[max_n];
int dp[max_n][4];

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &in[i]);
  }
  dp[0][0] = 0; // nastepna ma rosnac
  dp[0][1] = 0; // nastepna ma malec
  dp[0][2] = 1; // -inf
  dp[0][3] = 1; // +inf
  for (int i = 1; i < n; ++i) {
    dp[i][0] = dp[i - 1][3];
    if (in[i - 1] > in[i]) dp[i][0] = std::min(dp[i][0], dp[i - 1][1]);

    dp[i][1] = dp[i - 1][2];
    if (in[i - 1] < in[i]) dp[i][1] = std::min(dp[i][1], dp[i - 1][0]);

    dp[i][3] = 1 + std::min(dp[i - 1][2], dp[i - 1][0]);

    dp[i][2] = 1 + std::min(dp[i - 1][3], dp[i - 1][1]);
  }
  printf("%d\n", std::min(std::min(dp[n - 1][0], dp[n - 1][1]), std::min(dp[n - 1][2], dp[n - 1][3])));
  return 0;
}