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

const int MAXN = 50007;
const int duzo = 1e9+7;
int n;
int dp[MAXN][4];
//0 - zostawiam, ide w gore
//1 - zostawiam, ide w dol
//2 - uzywam, ide w gore
//3 - uzywam, ide w dol
int t[MAXN];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for (int i = 1; i <= n; i++){
        cin>>t[i];
        for (int j = 0; j < 4; j++){
            dp[i][j] = duzo;
        }
    }
    dp[1][0] = 0;
    dp[1][1] = 0;
    dp[1][2] = 1;
    dp[1][3] = 1;
    for (int i = 2; i <= n; i++){
        dp[i][0] = dp[i-1][3];
        if (t[i] > t[i-1]){
            dp[i][0] = min(dp[i][0], dp[i-1][1]);
        }
        dp[i][1] = dp[i-1][2];
        if (t[i] < t[i-1]){
            dp[i][1] = min(dp[i][1], dp[i-1][0]);
        }
        dp[i][2] = min(dp[i-1][1], dp[i-1][3])+1;
        dp[i][3] = min(dp[i-1][0], dp[i-1][2])+1;
    }
    int w = duzo;
    for (int i = 0; i < 4; i++){
        w = min(w, dp[n][i]);
    }
    cout<<w<<"\n";
}