#include <bits/stdc++.h>
using namespace std;
const int MAX = 2000005;
int pieknosc[MAX], next_greater_element[MAX];
int dp[MAX];
int n;
void stos_monotoniczny(){
vector<int> stos;
for(int i=2*n; i>=1; i--){
while(!stos.empty() && pieknosc[stos.back()] <= pieknosc[i]) stos.pop_back();
if(!stos.empty())next_greater_element[i] = stos.back();
stos.push_back(i);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i=1; i<=n; i++) cin >> pieknosc[i];
for(int i=n+1; i<=2*n; i++) pieknosc[i] = pieknosc[i - n];
stos_monotoniczny();
for(int i=2*n; i>=1; i--) dp[i] = dp[next_greater_element[i]] +1;
// for(int i=1; i<=2*n; i++) cout<<next_greater_element[i]<<" ";
int ans = 0;
for(int i=1; i<=n; i++) ans = max(ans, dp[i]);
cout << ans;
return 0;
}
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 | #include <bits/stdc++.h> using namespace std; const int MAX = 2000005; int pieknosc[MAX], next_greater_element[MAX]; int dp[MAX]; int n; void stos_monotoniczny(){ vector<int> stos; for(int i=2*n; i>=1; i--){ while(!stos.empty() && pieknosc[stos.back()] <= pieknosc[i]) stos.pop_back(); if(!stos.empty())next_greater_element[i] = stos.back(); stos.push_back(i); } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for(int i=1; i<=n; i++) cin >> pieknosc[i]; for(int i=n+1; i<=2*n; i++) pieknosc[i] = pieknosc[i - n]; stos_monotoniczny(); for(int i=2*n; i>=1; i--) dp[i] = dp[next_greater_element[i]] +1; // for(int i=1; i<=2*n; i++) cout<<next_greater_element[i]<<" "; int ans = 0; for(int i=1; i<=n; i++) ans = max(ans, dp[i]); cout << ans; return 0; } |
English