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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <vector>

using namespace std;

#define PB push_back


int main() {

    int n;
    int a;

    cin >> n;

    vector<int> input;


    for (int i = 0; i < n; ++i) {
        cin >> a;
        input.PB(a);
    }

    int counter = 1;
    int order = 2;
    // 1 - asc
    // 2 - unknown
    // 3 - desc


    vector<int> wrongs;

    for (int i = 1; i < input.size(); ++i) {

        int x1 = input[i - 1];
        int x2 = input[i];

        bool is_equal = x1 == x2;
        bool is_asc = x1 < x2;
        bool is_desc = x1 > x2;

        if (order == 1) {
            if (is_equal || is_asc) {
                counter++;
            } else {
                wrongs.PB(counter);
                counter = 0;
                order = 2;
            }
        }
        if (order == 3) {

            if (is_equal || is_desc) {
                counter++;
            } else {
                wrongs.PB(counter);
                counter = 0;
                order = 2;
            }
        }
        if (order == 2) {
            counter++;
            if( counter != 1){

            if (is_asc) {
                order = 1;
            }
            if (is_desc) {
                order = 3;
            }
            }
        }


    }
    wrongs.PB(counter);


    int result = 0;

    for (int wrong: wrongs) {
//        cout << wrong << endl;
        if (wrong > 2) {
            result += (wrong / 2);
        }
    }

    cout << result << endl;

    return 0;
}