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

using namespace std;

#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> para;
const ll inf = 1e18 + 7;
const ll maxN = 1e6 + 5;
const ll MOD = 1e9 + 7;

// dp[0 - rosn, 1 - mal][0 - bez zmiany, 1 ze zmiana]
int arr[maxN], n, dp[2][2][maxN];

int calc() {
    REP(i, n)
        REP(j, 2)
            dp[0][j][i] = dp[1][j][i] = maxN;
    dp[0][0][0] = dp[1][0][0] = 0;
    dp[0][1][0] = dp[1][1][0] = 1;
    RI(i, n - 1) {
        if (arr[i] > arr[i - 1]) {
            dp[0][0][i] = min(dp[1][0][i - 1], dp[1][1][i - 1]);
            dp[0][1][i] = 1 + dp[0][0][i];

            dp[1][0][i] = dp[0][1][i - 1];
            dp[1][1][i] = 1 + min(dp[0][1][i - 1], dp[0][0][i - 1]);
        }

        if (arr[i] < arr[i - 1]) {
            dp[0][0][i] = dp[1][1][i - 1];
            dp[0][1][i] = 1 + min(dp[1][0][i - 1], dp[1][1][i - 1]);

            dp[1][0][i] = min(dp[0][1][i - 1], dp[0][0][i - 1]);
            dp[1][1][i] = 1 + dp[1][0][i];
        }

        if (arr[i] == arr[i - 1]) {
            dp[0][0][i] = dp[1][1][i - 1];
            dp[0][1][i] = 1 + min(dp[1][0][i - 1], dp[1][1][i - 1]);

            dp[1][0][i] = dp[0][1][i - 1];
            dp[1][1][i] = 1 + min(dp[0][1][i - 1], dp[0][0][i - 1]);
        }
//        cout << i << ": " << dp[0][0][i] << " " << dp[0][1][i] << " " << dp[1][0][i] << " " << dp[1][1][i] << endl;
    }
    return min(dp[0][0][n - 1], min(dp[0][1][n - 1], min(dp[1][0][n - 1], dp[1][1][n - 1])));
}

int main() {
    cin >> n;
    REP(i, n) cin >> arr[i];

    cout << calc() << endl;
    return 0;
}