#include <bits/stdc++.h>
#define F(i, a, b) for(int i = (a); i <= (b); i++)
#define R(i, a, b) for(int i = (a); i >= (b); i--)
#define pb push_back
#define V vector
#define pii pair<int,int>
#define f first
#define s second
using namespace std;
int n;
int tab[2000009];
int bin[2000005][22];
int pot[30];
void pre(){
F(j, 1, 21){
F(i, 1, 2*n){
bin[i][j] = bin[bin[i][j-1]][j-1];
}
}
pot[0]=1;
F(i, 1, 21){
pot[i]=pot[i-1]*2;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
F(i, 1, n){
cin >> tab[i];
tab[n+i] = tab[i];
}
deque<pii> stos;
stos.pb({tab[2*n], 2*n});
bin[2*n][0] = 2*n;
R(i, 2*n-1, 1){
while(!stos.empty() && stos.back().f <= tab[i]){
stos.pop_back();
}
if(stos.empty()) bin[i][0] = 0;
else bin[i][0] = stos.back().s;
stos.pb({tab[i], i});
}
pre();
int ans = 1;
F(ind, 1, n){
int w=0;
int j = ind;
R(i, 21, 0){
if(bin[j][i] == j || bin[j][i] == 0) continue;
if(bin[j][i] < n+ind) j=bin[j][i],w+=pot[i];
}
ans = max(ans, w+1);
}
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #include <bits/stdc++.h> #define F(i, a, b) for(int i = (a); i <= (b); i++) #define R(i, a, b) for(int i = (a); i >= (b); i--) #define pb push_back #define V vector #define pii pair<int,int> #define f first #define s second using namespace std; int n; int tab[2000009]; int bin[2000005][22]; int pot[30]; void pre(){ F(j, 1, 21){ F(i, 1, 2*n){ bin[i][j] = bin[bin[i][j-1]][j-1]; } } pot[0]=1; F(i, 1, 21){ pot[i]=pot[i-1]*2; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; F(i, 1, n){ cin >> tab[i]; tab[n+i] = tab[i]; } deque<pii> stos; stos.pb({tab[2*n], 2*n}); bin[2*n][0] = 2*n; R(i, 2*n-1, 1){ while(!stos.empty() && stos.back().f <= tab[i]){ stos.pop_back(); } if(stos.empty()) bin[i][0] = 0; else bin[i][0] = stos.back().s; stos.pb({tab[i], i}); } pre(); int ans = 1; F(ind, 1, n){ int w=0; int j = ind; R(i, 21, 0){ if(bin[j][i] == j || bin[j][i] == 0) continue; if(bin[j][i] < n+ind) j=bin[j][i],w+=pot[i]; } ans = max(ans, w+1); } cout << ans; } |
English