#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e6 + 7;
int val[MAXN];
int dp[MAXN];
int n;
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int maxi = 0;
cin >> n;
for(int i = 0; i < n; i++){
cin >> val[i];
val[n + i] = val[i];
maxi = max(maxi, val[i]);
}
stack<int> s;
int it = 2*n-1;
while(val[it] != maxi) it--;
s.push(it);
dp[it] = 1;
for(int i = it; i >= 0; i--){
while(!s.empty() && val[s.top()] <= val[i]) s.pop();
if(val[i] == maxi)dp[i] = 1;
else dp[i] = 1 + dp[s.top()];
s.push(i);
}
int ans = 0;
for(int i = 0; i < 2*n; i++)ans = max(ans, dp[i]);
cout << ans;
}
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 MAXN = 2e6 + 7; int val[MAXN]; int dp[MAXN]; int n; int main(){ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int maxi = 0; cin >> n; for(int i = 0; i < n; i++){ cin >> val[i]; val[n + i] = val[i]; maxi = max(maxi, val[i]); } stack<int> s; int it = 2*n-1; while(val[it] != maxi) it--; s.push(it); dp[it] = 1; for(int i = it; i >= 0; i--){ while(!s.empty() && val[s.top()] <= val[i]) s.pop(); if(val[i] == maxi)dp[i] = 1; else dp[i] = 1 + dp[s.top()]; s.push(i); } int ans = 0; for(int i = 0; i < 2*n; i++)ans = max(ans, dp[i]); cout << ans; } |
English