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
32
33
34
35
36
37
#include <algorithm>
#include <cstdio>

constexpr int N = 50 * 1000;
constexpr int M = 1000 * 1000 * 1000;
int tab[N];

int main() {
    int n;
    scanf("%d", &n);
    int ch_incr = 0, ch_decr = 0, prev_incr, prev_decr, curr;
    scanf("%d", &prev_incr);
    prev_decr = prev_incr;
    for (int i = 1; i < n; ++i) {
        scanf("%d", &curr);
        if (curr <= prev_incr) {
            ch_incr++;
            prev_incr = M;
        }
        else {
            prev_incr = curr;
        }

        if (curr >= prev_decr) {
            ch_decr++;
            prev_decr = -M;
        }
        else {
            prev_decr = curr;
        }

        std::swap(prev_decr, prev_incr);
        std::swap(ch_decr, ch_incr);
    }

    printf("%d\n", std::min(ch_incr, ch_decr));
}