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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stdbool.h>

#define MAX_N 50007

int a[MAX_N];

int min(int a, int b) {
    if (a < b)
        return a;

    return b;
}

int main() {
    int n;
    scanf("%d", &n);

    for (int i = 0; i < n; ++i)
        scanf("%d", &a[i]);

    int aa = (a[0] == a[1]) ? MAX_N : 0;
    int ad = 1;
    int da = 1;
    int am = 1;
    int ma = 1;

    for (int i = 2; i < n; i++) {
        int taa, tad, tda, tam, tma;
        taa = MAX_N;
        tad = da + 1;
        tda = ad;
        tam = ma + 1;
        tma = am;

        if (a[i - 2] > a[i - 1])
            tad = min(aa + 1, tad);

        if (a[i - 2] < a[i - 1])
            tam = min(aa + 1, tam);

        if ((a[i - 2] > a[i - 1] && a[i - 1] < a[i]) ||
            (a[i - 2] < a[i - 1] && a[i - 1] > a[i]))
            taa = min(aa, taa);

        if (a[i - 1] < a[i])
            taa = min(da, taa);

        if (a[i - 1] > a[i])
            taa = min(ma, taa);

        aa = taa;
        ad = tad;
        da = tda;
        am = tam;
        ma = tma;
    }

    printf("%d\n", min(aa, min(ad, min(da, min(am, ma)))));

    return 0;
}