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;
typedef long long ll;
const int inf=1e9;
int main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin>>n;
    vector<int>v(2*n+1);

    for (int i=1; i<=n; i++) {
        cin>>v[i];
        v[i+n]=v[i];
    }
    vector<int>dp(2*n+1);

    vector<int>s;
    dp[2*n]=1;
    s.push_back(2*n);
    int res=0;
    for (int i=2*n-1; i>=1; i--) {
        while (!s.empty() and v[s.back()]<=v[i]) {
            s.pop_back();
        }
        if (!s.empty())
            dp[i]=dp[s.back()]+1;
        else
            dp[i]=1;
        s.push_back(i);
        res=max(res, dp[i]);
    }
    cout<<res<<'\n';
    return 0;
}