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

using namespace std;

const int H = 1000000 + 1;
const int L = -1000000 - 1;

int policzZmiany(vector<int>& seq) // assuming that seq starts from high signal
{
    int n = seq.size();
    int counter=0;
    int lastVal= seq[0];
    for (int ii=1; ii<n; ii++)
    {
        int newVal=seq[ii];
        if (ii%2==1 && newVal>=lastVal)
        {
            newVal = L;
            counter++;
        }
        else if (ii%2==0 && newVal<=lastVal)
        {
            newVal = H;
            counter++;
        }
        lastVal = newVal;
    }
    return counter;
}

int main()
{
    int n;
    cin>>n;

    vector<int> seq;
    for(int ii=0; ii<n; ii++)
    {
        int val;
        cin>>val;
        seq.push_back(val);
    }

    // seq starts from high signal
    int counterH = policzZmiany(seq);

    // seq starts from low signal - odwrocenie znaku elementow
    for (int ii=0; ii<n; ii++)
        seq[ii] = -seq[ii];
    int counterL = policzZmiany(seq);

    cout << min(counterH, counterL) << endl;
    return 0;
}