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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include<bits/stdc++.h>
using namespace std;

#define int int64_t

const int64_t mod = 1e9+7;
const int maxn = 1e6+10;
int inf;


/*
*/

int a[maxn];
int next_max[maxn];
int n;

deque<pair<int, int>> mq;
int sind;

void print_mq()
{
    /*cout << "sind=" << sind << " mq: ";
    for (auto it = mq.begin(); it != mq.end(); it++)
    {
        cout << it->first << "," << it->second << " ";
    }
    cout << endl;*/
}

int next_ind(int i)
{
    if (i < n) return i+1;
    return 1;
}

int prev_ind(int i)
{
    if (i > 1) return i-1;
    return n;
}

int calc_by_monotonic_queue()
{
    int max_size = 0;
    sind = 1;
    int lb = 0;
    for (int i = sind; i <= n; i++)
    {
        if (a[i] > lb)
        {
            lb = a[i];
            mq.push_back({a[i], i});
        }
    }
    max_size = mq.size();
    

    for ( ; sind <= n; sind++)
    {
        print_mq();
        // przesuniecie
        if (mq.begin()->second == sind)
        {
            // usuwamy poczatek
            mq.pop_front();
            // dodajemy na poczatku
            int start_from = 0;
            if (mq.size() >= 1)
            {   
                start_from = prev_ind(mq.begin()->second);
            }
            else
            {
                start_from = next_max[next_ind(sind)];
                if (start_from == sind)
                {
                    mq.push_front({a[sind], sind});
                    start_from = prev_ind(start_from);
                }
            }
            //cout << "start_from=" << start_from << endl;
            for (int i = start_from; i != sind; i = prev_ind(i))
            {
                while (mq.size() >= 1 && a[i] >= mq.begin()->first)
                {
                    mq.pop_front();
                }
                mq.push_front({a[i], i});
            }
        }
        // wrzucamy aktualny sind na koniec
        if ((mq.end()-1)->first < a[sind])
        {
            mq.push_back({a[sind], sind});
        }
        int current_size = mq.size();
        max_size = max(max_size, current_size);
    }

    return max_size;
}

void tc()
{
    cin >> n;
    int maxv = 0;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        maxv = max(maxv, a[i]);
    }

    next_max[n+1] = -1;
    for (int i = n; i >= 1; i--)
    {
        if (a[i] == maxv)
        {
            next_max[i] = i;
        }
        else
        {
            next_max[i] = next_max[i+1];
        }
    }
    for (int i = n; i >= 1; i--)
    {
        if (a[i] == maxv)
        {
            next_max[i] = i;
        }
        else
        {
            next_max[i] = next_max[next_ind(i)];
        }
    }

    int ans = calc_by_monotonic_queue();
    cout << ans << endl;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
	inf = numeric_limits<int>::max()/2;

    int T = 1;
    //cin >> T;
    while (T--) tc();

    return 0;
}