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
#include <bits/stdc++.h>
//#define int long long
#define ll long long
#define db double
#define fi first
#define se second
#define pii pair<int,int>
#define vi vector<int>

using namespace std;

const int maxn=5e5,inf=1e9; 
int n,f[maxn+5][2],a[maxn+5]; 

int main() {
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]); 
	f[1][0]=0;
	f[1][1]=1; 
	for (int i=2;i<=n;i++) {
		for (int j=0;j<2;j++) f[i][j]=inf; 
		for (int j=0;j<2;j++) {
			for (int k=0;k<2;k++) {
				if (j==0 && k==0) {
					if (i%2==0 && a[i-1]<=a[i]) continue ;
					if (i%2==1 && a[i-1]>=a[i]) continue ; 
				}
				if (f[i-1][j]==inf) continue ;
				f[i][k]=min(f[i][k],f[i-1][j]+k); 
			}
		}
	}
	int ans=min(f[n][0],f[n][1]); 
	f[1][0]=0;
	f[1][1]=1; 
	for (int i=2;i<=n;i++) {
		for (int j=0;j<2;j++) f[i][j]=inf; 
		for (int j=0;j<2;j++) {
			for (int k=0;k<2;k++) {
				if (j==0 && k==0) {
					if (i%2==0 && a[i-1]>=a[i]) continue ;
					if (i%2==1 && a[i-1]<=a[i]) continue ; 
				}
				if (f[i-1][j]==inf) continue ;
				f[i][k]=min(f[i][k],f[i-1][j]+k); 
			}
		}
	}
	cout<<min(ans,min(f[n][0],f[n][1]))<<'\n'; 	
	return 0;
}