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
//: Potyczki Algorytmiczne 2022
// ORN
#include <cstdio>
#include <queue>

constexpr int INF{ 1'000'000'000 };
constexpr int UPPER{  1'000'000 + 1};
constexpr int LOWER{ -1'000'000 - 1 };

int a[50000];

struct State
{
    int index;
    int prev;
    bool dir;
    int score;
};

int main()
{
    int n{};
    std::scanf("%d", &n);
    int min{ INF };

    for (int i = 0; i < n; ++i)
    {
        std::scanf("%d", &a[i]);
    }

    std::queue<State> Q{};

    State first{}, second{};

    if (a[1] > a[0])
    {
        first.index = 2;
        first.prev = a[1];
        first.dir = false;

        second.index = 2;
        second.prev = LOWER;
        second.dir = true;
        second.score = 1;
    }
    else if (a[1] < a[0])
    {
        first.index = 2;
        first.prev = a[1];
        first.dir = true;

        second.index = 2;
        second.prev = UPPER;
        second.dir = false;
        second.score = 1;
    }
    else if (a[1] == a[0])
    {
        first.index = 2;
        first.prev = UPPER;
        first.dir = false;
        first.score = 1;

        second.index = 2;
        second.prev = LOWER;
        second.dir = true;
        second.score = 1;
    }

    Q.push(first);
    Q.push(second);

    while (!Q.empty())
    {
        const State curr{ Q.front() }; Q.pop();

        if (curr.index == n)
        {
            if (curr.score < min)
            {
                min = curr.score;
            }
        }
        else
        {
            State next{};

            if ((a[curr.index] > curr.prev) && (curr.dir == false))
            {
                next.index = curr.index + 1;
                next.prev = LOWER;
                next.dir = true;
                next.score = curr.score + 1;
            }
            else if ((a[curr.index] < curr.prev) && (curr.dir == true))
            {
                next.index = curr.index + 1;
                next.prev = UPPER;
                next.dir = false;
                next.score = curr.score + 1;
            }
            else if ((a[curr.index] == curr.prev) && (curr.dir == false))
            {
                next.index = curr.index + 1;
                next.prev = LOWER;
                next.dir = true;
                next.score = curr.score + 1;
            }
            else if ((a[curr.index] == curr.prev) && (curr.dir == true))
            {
                next.index = curr.index + 1;
                next.prev = UPPER;
                next.dir = false;
                next.score = curr.score + 1;
            }
            else
            {
                next.index = curr.index + 1;
                next.prev = a[curr.index];
                next.dir = !curr.dir;
                next.score = curr.score;
            }

            Q.push(next);
        }
    }

    std::printf("%d\n", min);
}