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
#include <bits/stdc++.h>
using namespace std;
#define TF(i, p, q) for(int i = (p); i <= (q); ++i)
#define TR(i, q, p) for(int i = (q); i >= (p); --i)
#define V vector
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define th third
#define rz resize
#define all(V) V.begin(), V.end()
#define rall(V) V.rbegin(), V.rend()
#define sz(V) (int)(V.size())
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b);
typedef pair<int,int> ii;
typedef queue<int> qi;
typedef queue<ii> qii;
typedef long long ll; 
typedef vector<int> vi;
typedef vector<vector<int>>vvi;
typedef vector<vector<bool>>vvb;
typedef vector<bool> vb;
typedef vector<ii> vii;
constexpr int inf = 1e9 + 9;
int logcalc(int n){
    int x = 1, c = 0;
    while(x < n){
        x <<= 1;
        c++;
    }
    return c;
}
void solve(){
    int n; cin >> n;
    vi tab(n+n);
    int mx = n+n;
    int LOG = logcalc(n+n);
    vvi jp(n+n, vi(LOG+1));
    TF(i, 0, n-1){
        cin >> tab[i];
        tab[i+n] = tab[i];
    }
    int r = 0; stack<int>s;
    while(r < mx){
        while(!s.empty() and tab[s.top()] < tab[r]){
            jp[s.top()][0] = r;
            s.pop();
        }
        s.push(r); r++;
    }
    while(!s.empty()){
        jp[s.top()][0] = inf;
        s.pop();
    }
    TF(j, 1, LOG){
        TF(i, 0, mx-1){
            jp[i][j] = (jp[i][j-1] == inf ? inf : jp[jp[i][j-1]][j-1]);
        }
    }
    int score = 0;
    TF(i, 0, n-1){
        int cp = i;
        int val = 0;
        TR(j, LOG, 0){
            if(jp[cp][j]-i+1 <= n){
                cp = jp[cp][j];
                val += 1<<j;
            }
        }
        cmax(score, val+1);
    }
    cout << score;
}
int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); solve();
}