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
#include<iostream>
using namespace std;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,current=0, wynik=0, ujcur=0, ujwyn=0;
    cin>>n;
    int x;
    int last;
    int alast;
    cin>>last;
    alast=last;
    for(int i=1;i<n;++i){
        cin>>x;
        if(last<x){
            ++current;
        }
        if(alast<x){
            ++ujcur;
        }
        if(last>x){
            --current;
        }
        if(alast>x){
            --ujcur;
        }
        if(current>1){
            current=0;
            ++wynik;
        }
        if(current<0){
            current=1;
            ++wynik;
        }
        if(ujcur>0){
            ujcur=-1;
            ++ujwyn;
        }
        if(ujcur<-1){
            ujcur=0;
            ++ujwyn;
        }
        if(last==x){
            ++wynik;
            ++ujwyn;
            if(current==1){
                current=0;
                last=-1000007;
            }
            if(current==0){
                current=1;
                last=1000007;
            }
            if(ujcur==-1){
                ujcur=0;
                alast=1000007;
            }
            if(ujcur==0){
                ujcur=-1;
                alast=-1000007;
            }
        }else{
            last=x;
            alast=x;
        }
    }
    cout<<min(wynik,ujwyn);
    return 0;
}