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
#include <iostream>
using namespace std;

int MAX_INT = 1000*1000*10;
int MIN_INT = -MAX_INT;
int changes_req(bool, int, const int[]);

int main() {
    int n;
    int s[50001];
    cin >> n;
    for(int i = 0; i < n; ++i) {
        cin >> s[i];
    }
    int upfirst_changes = 0;
    int downfirst_changes = 0;

    upfirst_changes = changes_req(false, n, s);
    downfirst_changes = changes_req(true, n, s);

    if (upfirst_changes < downfirst_changes) {
        cout << upfirst_changes << endl;
    } else {
        cout << downfirst_changes << endl;
    }
    return 0;
}

int changes_req(bool phase, int n, const int* s) {
    int changes_req = 0;
    int prevsound = s[0];
    for(int i = 1; i < n; ++i) {
        if (i % 2 == phase) {
            // this sound must be lower than previous
            if (s[i] >= prevsound) {
                // cout << "PHASE: " << phase << " | Change req at " << i << endl;
                changes_req += 1;
                prevsound = MIN_INT;
            } else {
                prevsound = s[i];
            }
        } else {
            // this sound must be higher than previous
            if (s[i] <= prevsound) {
                // cout << "PHASE: " << phase << " | Change req at " << i << endl;
                changes_req += 1;
                prevsound = MAX_INT;
            } else {
                prevsound = s[i];
            }
        }
    }
    return changes_req;
}