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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <bits/stdc++.h>

using namespace std;

const int MIN = -1'000'000'000;
const int MAX = 1'000'000'000;

struct TestCase
{
  vector<int> sequence;
};

TestCase read_test_case();
void solve_test_case(const TestCase &);

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve_test_case(read_test_case());
}

TestCase read_test_case()
{
  TestCase tc;
  size_t sequence_length;
  cin >> sequence_length;

  tc.sequence.resize(sequence_length);
  for (auto &x : tc.sequence)
    cin >> x;

  return tc;
}

size_t changes_to_alternate(const vector<int>& sequence, bool decreases)
{
  size_t changes = 0;
  vector<int> seq;
  seq.push_back(sequence[0]);

  for (size_t i{1}; i < sequence.size(); i++)
  {
    if (decreases)
    {
      if (seq.back() > sequence[i])
        seq.push_back(sequence[i]);
      else
      {
        seq.push_back(MIN);
        changes++;
      }
    }
    else
    {
      if (seq.back() < sequence[i])
        seq.push_back(sequence[i]);
      else
      {
        seq.push_back(MAX);
        changes++;
      }
    }

    decreases = !decreases;
  }
  return changes;
}

void solve_test_case(const TestCase &tc)
{
  size_t start_decreasing = changes_to_alternate(tc.sequence, true);
  size_t start_increasing = changes_to_alternate(tc.sequence, false);

  cout << min(start_decreasing, start_increasing) << "\n";
}