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
#include <bits/stdc++.h>

using namespace std;
const int MX = 2e6+9;
int nxt[MX], dp[MX];
vector<int>vect = {0};
vector<pair<int, int>>st = {{MX, 0}};
int get(){
    int x = 0;
    char c = getchar_unlocked();
    while(c < '0' || c > '9') c = getchar_unlocked();
    while(c >= '0' && c <= '9'){
        x = x*10+c-'0';
        c = getchar_unlocked();
    }
    return x;
}
int main(){
    int n = get(), res = 0;
    for(auto i = 1; i <= n; ++i){
        int x = get();
        vect.push_back(x);
    }
    for(auto i = 1; i <= n; ++i) vect.push_back(vect[i]);
    for(auto i = 2*n; i; --i){
        auto [h, j] = st.back();
        while(h <= vect[i]){
            st.pop_back();
            h = st.back().first;
            j = st.back().second;
        }
        dp[i] = dp[j]+1;
        if(i <= n) res = max(res, dp[i]);
        st.emplace_back(vect[i], i);
    }
    cout << res;
}