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

using namespace std;

const int N_MAX = 5e4 + 1;
int n;
int a[N_MAX-1];
int dp[N_MAX][2]; // 0 - gora, 1 - dol

int main(void)
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin >> n;
    for(int i=0; i<n; ++i){
        cin >> a[i];
        dp[i+1][0] = dp[i+1][1] = 1e7;
    }

    for(int i=1; i<n; ++i){
        if(a[i-1] < a[i]){
            dp[i][0] = min(dp[i][0], dp[i-1][1]);
            dp[i][1] = dp[i+1][0] = min(dp[i][1], dp[i-1][0] + 1);
        }
        else if(a[i-1] > a[i]){
            dp[i][0] = dp[i+1][1] = min(dp[i][0], dp[i-1][1] + 1);
            dp[i][1] = min(dp[i][1], dp[i-1][0]);
        }
        else{
            dp[i][0] = dp[i+1][1] = min(dp[i][0], dp[i-1][1] + 1);
            dp[i][1] = dp[i+1][0] = min(dp[i][1], dp[i-1][0] + 1);
            i++;
        }
    }

    cout << min(dp[n-1][0], dp[n-1][1]) << '\n';
}