#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ve vector
#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
void solve(){
int n;
cin >> n;
ve<int> v(n);
for(auto& i : v)
cin >> i;
v.insert(v.end(), v.begin(), v.begin()+n);
stack<int> st;
ve<int> dp(2*n, 1);
ve<int> nxt(2*n, -1);
for(int i = 2*n-1; i >= 0; i--){
while(!st.empty() && v[st.top()] <= v[i])
st.pop();
if(!st.empty())
nxt[i] = st.top();
st.push(i);
}
for(int i = 2*n-1; i >= 0; i--){
if(nxt[i] == -1) continue;
dp[i] = dp[nxt[i]] + 1;
}
cout << *max_element(all(dp)) << "\n";
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int T = 1;
// cin >> T;
while(T--) solve();
}
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 | #include <bits/stdc++.h> using namespace std; #define ll long long #define ve vector #define fi first #define se second #define pb push_back #define all(x) begin(x), end(x) typedef pair<int, int> pii; typedef pair<ll, ll> pll; void solve(){ int n; cin >> n; ve<int> v(n); for(auto& i : v) cin >> i; v.insert(v.end(), v.begin(), v.begin()+n); stack<int> st; ve<int> dp(2*n, 1); ve<int> nxt(2*n, -1); for(int i = 2*n-1; i >= 0; i--){ while(!st.empty() && v[st.top()] <= v[i]) st.pop(); if(!st.empty()) nxt[i] = st.top(); st.push(i); } for(int i = 2*n-1; i >= 0; i--){ if(nxt[i] == -1) continue; dp[i] = dp[nxt[i]] + 1; } cout << *max_element(all(dp)) << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int T = 1; // cin >> T; while(T--) solve(); } |
English