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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include<iostream>
#include<vector>

using namespace std;

int INF=1e9;

pair<vector<int>,int> sequence_up(vector<int>& tab, int n)
{
    vector<int> up_sounds(n+1);
    int counter=0;

    up_sounds[1]=tab[1];
    if(tab[1]==-INF)
    {
        up_sounds[1]=INF;
        counter++;
    }

    for(int i=2;i<=n;i++)
    {
        up_sounds[i]=tab[i];
        if(i%2==0)
        {
            //down

            if(up_sounds[i]>=up_sounds[i-1])
            {
                up_sounds[i]=-INF;
                counter++;
            }

        }
        else
        {
            //up

            if(up_sounds[i]<=up_sounds[i-1])
            {
                up_sounds[i]=INF;
                counter++;
            }
        }
    }

    return make_pair(up_sounds,counter);
}

pair<vector<int>,int> sequence_down(vector<int>& tab, int n)
{
    vector<int> down_sounds(n+1);
    int counter=0;

    down_sounds[1]=tab[1];
    if(tab[1]==INF)
    {
        down_sounds[1]=-INF;
        counter++;
    }

    for(int i=2;i<=n;i++)
    {
        down_sounds[i]=tab[i];
        if(i%2!=0)
        {
            //down

            if(down_sounds[i]>=down_sounds[i-1])
            {
                down_sounds[i]=-INF;
                counter++;
            }

        }
        else
        {
            //up

            if(down_sounds[i]<=down_sounds[i-1])
            {
                down_sounds[i]=INF;
                counter++;
            }
        }
    }

    return make_pair(down_sounds,counter);
}

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

    vector<int> tab(n+1);

    for(int i=1;i<=n;i++)
    {
        cin>>tab[i];
    }

    pair<vector<int>,int> up_sound=sequence_up(tab,n);
    pair<vector<int>,int> down_sound=sequence_down(tab,n);

    if(up_sound.second<=down_sound.second)
    {
        cout<<up_sound.second<<endl;

        /*vector<int> up_vec=up_sound.first;

        for(int i=1;i<up_vec.size();i++)
        {
            cout<<up_vec[i]<<" ";
        }

        cout<<endl;*/
    }
    else
    {
        cout<<down_sound.second<<endl;

        /*vector<int> down_vec=down_sound.first;

        for(int i=1;i<=n;i++)
        {
            cout<<down_vec[i]<<" ";
        }

        cout<<endl;*/
    }

    return 0;
}