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

typedef long long LL;

const int N = 50000+2;
LL arr[N];
LL d[N][4];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);   
      
    int n;
    cin >> n;
    
    for(int i = 0; i < n; ++i)
        cin >> arr[i+1];
    d[0][1] = d[0][0] = d[0][2] = d[0][3] = 0;
    for(int i = 1; i <= n; ++i) {
        d[i][0] = min(d[i-1][1], d[i-1][2])+1;
        d[i][1] = min(d[i-1][0], d[i-1][3])+1;
        d[i][2] = min(d[i-1][0], arr[i-1] > arr[i] ? d[i-1][3] : 1000000);
        d[i][3] = min(d[i-1][1], arr[i-1] < arr[i] ? d[i-1][2] : 1000000);
    }
    LL res = min(min(d[n][0], d[n][1]), min(d[n][2], d[n][3]));
    cout << res << '\n';
    return 0;
}