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

#define pb push_back
#define st first
#define nd second
#define mod 1000696969

typedef long long ll;

using namespace std;

int n;
int tab[50005];

int dp[5][50005];

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

    cin>>n;
    for(int i=0; i<n; i++)
    {
        cin>>tab[i];
    }
    dp[0][0]=1;
    dp[3][0]=1;
    for(int i=1; i<n; i++)
    {
        dp[0][i]=min(dp[2][i-1],dp[3][i-1])+1;
        dp[1][i]=dp[3][i-1];
        dp[2][i]=dp[0][i-1];
        dp[3][i]=min(dp[0][i-1],dp[1][i-1])+1;
        if(tab[i-1]<tab[i])
        {
            dp[1][i]=min(dp[1][i],dp[2][i-1]);
        }
        else if(tab[i-1]>tab[i])
        {
            dp[2][i]=min(dp[2][i],dp[1][i-1]);
        }
    }
    cout<<min(min(dp[0][n-1],dp[1][n-1]),min(dp[2][n-1],dp[3][n-1]));

    return 0;
}