#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define ll long long
#define ld long double
#define pll pair<ll,ll>
#define pb push_back
#define F first
#define S second
#define INF (ll)2e18
#define pii pair<int,int>
#define int ll
void solve() {
int n; cin >> n;
vector<int> a(2*n);
for (int i = 0; i < n; i++) cin >> a[i], a[i+n] = a[i];
int ans = 0;
vector<int> g(2*n);
vector<int> st;
for (int i = 0; i < 2*n; i++) {
while (!st.empty() && a[st.back()] < a[i]) {
g[st.back()] = i;
st.pop_back();
}
st.push_back(i);
}
vector<vector<int>> jp(2*n, vector<int>(20));
for (int i = 2*n-1; i >= 0; i--) {
if (g[i] == 0) g[i] = -1;
jp[i][0] = g[i];
for (int j = 1; j < 20; j++) {
if (jp[i][j-1] == -1) jp[i][j] = -1;
else jp[i][j] = jp[jp[i][j-1]][j-1];
}
}
for (int i = 0; i < n; i++) {
int cur = 0;
int j = i;
for (int k = 19; k >= 0; k--) {
if (jp[j][k] != -1 && jp[j][k] < i+n) {
j = jp[j][k];
cur += (1 << k);
}
}
ans = max(ans, cur+1);
}
cout << ans << '\n';
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
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 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 74 75 | #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; using namespace std; template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #define all(a) (a).begin(),(a).end() #define allr(a) (a).rbegin(),(a).rend() #define ll long long #define ld long double #define pll pair<ll,ll> #define pb push_back #define F first #define S second #define INF (ll)2e18 #define pii pair<int,int> #define int ll void solve() { int n; cin >> n; vector<int> a(2*n); for (int i = 0; i < n; i++) cin >> a[i], a[i+n] = a[i]; int ans = 0; vector<int> g(2*n); vector<int> st; for (int i = 0; i < 2*n; i++) { while (!st.empty() && a[st.back()] < a[i]) { g[st.back()] = i; st.pop_back(); } st.push_back(i); } vector<vector<int>> jp(2*n, vector<int>(20)); for (int i = 2*n-1; i >= 0; i--) { if (g[i] == 0) g[i] = -1; jp[i][0] = g[i]; for (int j = 1; j < 20; j++) { if (jp[i][j-1] == -1) jp[i][j] = -1; else jp[i][j] = jp[jp[i][j-1]][j-1]; } } for (int i = 0; i < n; i++) { int cur = 0; int j = i; for (int k = 19; k >= 0; k--) { if (jp[j][k] != -1 && jp[j][k] < i+n) { j = jp[j][k]; cur += (1 << k); } } ans = max(ans, cur+1); } cout << ans << '\n'; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); int t = 1; //cin >> t; while (t--) { solve(); } return 0; } |
English