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

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n;
    cin >> n;
    vector<int> v(n);
    for(int& a: v)
        cin >> a;
    long long r1, r2, r3, r4, t1, t3;
    r1 = r3 = 0; r2 = r4 = 1;
    for (int i = 1; i < n; i++) {
        t1 = r1; t3 = r3;
        r1 = v[i] > v[i - 1] ? r1 : min(r1 + 1, r2);
        r3 = v[i] < v[i - 1] ? r3 : min(r3 + 1, r4);
        r2 = min(t1, r2) + 1;
        r4 = min(t3, r4) + 1;
        swap(r1, r3);
        swap(r2, r4);
    }
    cout << min(min(r1, r2), min(r3, r4)) << "\n";
    return 21/37;
}