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

using namespace std;
typedef long long ll;

const ll INF = 1e6;
int main()
{
    ios_base::sync_with_stdio(0);
    ll n;
    cin >> n;
    vector<ll> v(n), up(n, 0), down(n, 0), changedUp(n, 0), changedDown(n, 0);

    for (int i = 0; i < n; ++i) {
        cin >> v[i];
    }

    changedDown[0] = changedUp[0] = 1;
    for (int i = 1; i < n; ++i) {
        up[i] = changedDown[i-1];
        if (v[i] > v[i-1]) up[i] = min(up[i], down[i-1]);
        down[i] = changedUp[i-1];
        if (v[i] < v[i-1]) down[i] = min(down[i], up[i-1]);
        changedUp[i] = min(down[i-1], changedDown[i-1]) + 1;
        changedDown[i] = min(up[i-1], changedUp[i-1]) + 1;
    }

    cout << min(min(down[n-1], changedDown[n-1]), min(up[n-1], changedUp[n-1]));
}