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

using namespace std;

int n, x, err, err1, err2, err3, err4, lvl;
deque <int> q1;
deque <int> q2;
deque <int> q3;
deque <int> q4;

int solve (deque <int> q)
{
    err=0;
    lvl=0;
    for (int i=1; i<n; i++)
    {
        if (q[i-1]==q[i])
        {
            q[i] = (q[i-1]>q[i+1] ? q[i-1]+1 : q[i+1]+1);
            err++;
            lvl++;
        }
        else if (q[i]>q[i-1])
        {
            if (lvl==0)
                lvl++;
            else
            {
                q[i] = (q[i-1]<q[i+1] ? q[i-1]-1 : q[i+1]-1);
                lvl--;
                err++;
            }
        }
        else
        {
            {
                if (lvl==1)
                    lvl--;
                else
                {
                    q[i] = (q[i-1]>q[i+1] ? q[i-1]+1 : q[i+1]+1);
                    lvl++;
                    err++;
                }
            }
        }
    }
    return err;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie();
    cout.tie();

    cin>>n;



    for (int i=0; i<n; i++)
    {
        cin>>x;
        q1.push_back(x);
        q2.push_back(-1*x);
        q3.push_front(x);
        q4.push_front(-1*x);
    }


    q1.push_back(0);
    q2.push_back(0);
    q3.push_back(0);
    q4.push_back(0);


    err1 = solve(q1);
    err2 = solve(q2);
    err3 = solve(q3);
    err4 = solve(q4);

    cout<<min({err1, err2, err3, err4})<<'\n';
}