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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <bits/stdc++.h>
// Tymon Tumialis
using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, l = 0, r = 2;
    const int odp_min = pow(10, 9) * -1, odp_max = pow(10, 9);
    bool more = true;
    cin >> n;
    vector<int> V(n);
    vector<int> Res(n);
    for (int i = 0; i < n; i++)
    {
        cin >> V[i];
        Res[i] = V[i];
    }

    while (r < n)
    {
        if (more)
        {
            if (Res[l] > Res[l + 1])
            {
                if (Res[l + 1] >= Res[r])
                {
                    if (Res[r] <= Res[r + 1])
                    {
                        Res[r] = odp_max;
                        r++;
                        l++;
                        more ^= 1;
                    }
                    else
                        Res[l + 1] = odp_min;
                }
            }
            else if (Res[l] <= Res[l + 1])
                Res[l + 1] = odp_min;
        }
        else if (!more)
        {
            if (Res[l] < Res[l + 1])
            {
                if (Res[l + 1] <= Res[r])
                    Res[r] = odp_min;
            }
            else if (Res[l + 1] >= Res[r])
                Res[l + 1] = odp_min;
        }
        l++;
        r++;
        more ^= 1;
    }
    int wynik = 0;
    for (int i = 0; i < n; i++)
    {
        if (Res[i] != V[i])
            wynik++;
        Res[i] = V[i];
    }
    r = 2;
    l = 0;
    more = false;
    while (r < n)
    {
        if (more)
        {
            if (Res[l] > Res[l + 1])
            {
                if (Res[l + 1] >= Res[r])
                {
                    if (Res[r] <= Res[r + 1])
                    {
                        Res[r] = odp_max;
                        r++;
                        l++;
                        more ^= 1;
                    }
                    else
                        Res[l + 1] = odp_min;
                }
            }
            else if (Res[l] <= Res[l + 1])
                Res[l + 1] = odp_min;
        }
        else if (!more)
        {
            if (Res[l] < Res[l + 1])
            {
                if (Res[l + 1] <= Res[r])
                    Res[r] = odp_min;
            }
            else if (Res[l] >= Res[l + 1])
                Res[l] = odp_min;
        }
        l++;
        r++;
        more ^= 1;
    }
    int wynikk = 0;
    for (int i = 0; i < n; i++)
        if (Res[i] != V[i])
            wynikk++;

    cout << min(wynik, wynikk);

    return 0;
}