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
#include <bits/stdc++.h>

using namespace std;

#define MAX_VAL 1000000
#define MIN_VAL -1000000

int main() {
    int n, prevMin, prevMax;
    int r1 = 0, r2 = 0;
    cin >> n >> prevMin;
    prevMax = prevMin;
    if(prevMin == MAX_VAL) {
        prevMin = MIN_VAL;
        r1 += 1;
    }
    if(prevMax == MIN_VAL) {
        prevMax = MAX_VAL;
        r2 += 1;
    }

    for(int i = 1; i < n; ++i) {
        int x;
        cin >> x;
        if(x <= prevMin) {
            prevMin = MAX_VAL;
            r1 += 1;
        } else {
            prevMin = x;
        }

        if(x >= prevMax) {
            prevMax = MIN_VAL;
            r2 += 1;
        } else {
            prevMax = x;
        }

        swap(prevMax, prevMin);
        swap(r1, r2);
    }

    cout << min(r1, r2);
}