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
#include <iostream>
#include <vector>

using namespace std;

constexpr int INF = 1e9;
vector <int> tab;

inline int check(int mult) {// the first is up
	int ans = 0;
	vector <int> T = tab;
	for(int i = T.size() - 2; i >= 0; i--) {
		if(T[i] * mult - T[i + 1] * mult <= 0) {
			T[i] = INF * mult;
			ans++;
		}
		mult *= -1;
	}
	return ans;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n, ans = INF; cin >> n;
	for(int i = 0; i < n; i++) {
		int a; cin >> a;
		tab.push_back(a);
	}
	ans = check(-1);
	ans = min(ans, check(1));
	tab.push_back(tab.back());//making the necessity to change the first element
	ans = min(ans, check(1));
	ans = min(ans, check(-1));
	cout << ans << "\n";
	return 0;
}