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

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<ll,ll> pl;

const int max_n = 5e4+7;
const int I_max = 1e9;
const int I_min = -1e9;
int arr[max_n];

int main(){

  ios::sync_with_stdio(0);
  cin.tie(0);

  int n;
  cin >> n;

  for(int i = 1; i <= n; i++) cin >> arr[i];

  int ans = 0;
  if(arr[1] == arr[2]){
    arr[2] = I_max;
    ans++;
  }

  for(int i = 3; i <= n; i++){
    if(arr[i] == arr[i-1]){
      ans++;
      if(arr[i-1] > arr[i-2]) arr[i] = I_min;
      else arr[i] = I_max;
    }
    else if(arr[i] > arr[i-1] && arr[i-1] > arr[i-2]){
      arr[i] = I_min;
      ans++;
    }
    else if(arr[i] < arr[i-1] && arr[i-1] < arr[i-2]){
      arr[i] = I_max;
      ans++;
    }
  }
  cout << ans;

  return 0;
}

//g++ -O3 -static -Wall .cpp -std=c++17