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

const int max_n = 50002;
int n, a, mina, minak, minbk, minb, last;
int v[max_n], vk[max_n], vbk[max_n];
int v2[max_n];

int main(){
    ios_base::sync_with_stdio(0); cin.tie(NULL);
    cin>>n;
    for(int i=0; i<n; i++){
        cin >> a;
        v[i]=a; v2[i]=a;
        vk[n-i-1]=a;
        vbk[n-i-1]=a;
    }
    for(int i=1; i<n-1; i++){
        if(i%2==1){
            if(v[i]<v[i-1]){
                mina++;
                v[i]=max(v[i-1], v[i+1])+1;
            }
            if(i!=1 && v[i]>v[i-1]) continue;
        }
        if(i%2==0){
            if(v[i]>v[i-1]){
                mina++;
                v[i]=min(v[i-1], v[i+1])-1;
            }
            if(v[i]<v[i-1]) continue;
        }
    }

    for(int i=1; i<n-1; i++){
        if(i%2==1){
            if(vk[i]<vk[i-1]){
                minak++;
                vk[i]=max(vk[i-1], vk[i+1])+1;
            }
            if(i!=1 && vk[i]>vk[i-1]) continue;
        }
        if(i%2==0){
            if(vk[i]>vk[i-1]){
                minak++;
                vk[i]=min(vk[i-1], vk[i+1])-1;
            }
            if(vk[i]<vk[i-1]) continue;
        }
    }

    for(int i=1; i<n-1; i++){
        if(i%2==0){
            if(v[i]<v[i-1]){
                minb++;
                v[i]=max(v[i-1], v[i+1])+1;
            }
            if(i!=1 && v[i]>v[i-1]) continue;
        }
        if(i%2==1){
            if(v[i]>v[i-1]){
                minb++;
                v[i]=min(v[i-1], v[i+1])-1;
            }
            if(v[i]<v[i-1]) continue;
        }
    }

    for(int i=1; i<n-1; i++){
        if(i%2==0){
            if(vbk[i]<vbk[i-1]){
                minbk++;
                vbk[i]=max(vbk[i-1], vbk[i+1])+1;
            }
            if(i!=1 && vbk[i]>vbk[i-1]) continue;
        }
        if(i%2==1){
            if(vbk[i]>vbk[i-1]){
                minbk++;
                vbk[i]=min(vbk[i-1], vbk[i+1])-1;
            }
            if(vbk[i]<vbk[i-1]) continue;
        }
    }
    cout<<min(min(mina, minak), min(minb, minbk));
}