#include <bits/stdc++.h>
using namespace std;
int wyn = 0;
int to[1000009];
int dp[1000009];
long long getint() {
long long nm;
char c = getchar_unlocked();
while (c<'0' or c>'9') c = getchar_unlocked();
nm = c - '0';
c = getchar_unlocked();
while (c >= '0' and c <= '9') {
nm *= 10;
nm += (c - '0');
c = getchar_unlocked();
}
return nm;
}
void putint(long long nm) {
if (nm >= 10) putint(nm / 10);
putchar_unlocked('0' + nm % 10);
}
int main() {
int n = getint();
vector<int> a(n);
for (auto &i : a) i = (int)getint();
fill(to, to + n + 9, -1);
vector<int> st;
for (int i = 2 * n - 1; i >= 0; i--) {
int ind = i % n;
while (st.size() and a[st.back() % n] <= a[ind]) st.pop_back();
if (i < n and st.size()) {
to[ind] = st.back() % n;
}
st.push_back(i);
}
for (int i = 0; i < n; i++) {
if (dp[i]) {
wyn = max(wyn, dp[i]);
continue;
}
vector<int> prv;
int ind = i;
while (dp[ind] == 0 and to[ind] != -1) {
prv.push_back(ind);
ind = to[ind];
}
if (to[ind] == -1) dp[ind] = 1;
for (int j = prv.size() - 1; j >= 0; j--) {
dp[prv[j]] = dp[to[prv[j]]] + 1;
}
wyn = max(wyn,dp[i]);
}
putint(wyn);
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 | #include <bits/stdc++.h> using namespace std; int wyn = 0; int to[1000009]; int dp[1000009]; long long getint() { long long nm; char c = getchar_unlocked(); while (c<'0' or c>'9') c = getchar_unlocked(); nm = c - '0'; c = getchar_unlocked(); while (c >= '0' and c <= '9') { nm *= 10; nm += (c - '0'); c = getchar_unlocked(); } return nm; } void putint(long long nm) { if (nm >= 10) putint(nm / 10); putchar_unlocked('0' + nm % 10); } int main() { int n = getint(); vector<int> a(n); for (auto &i : a) i = (int)getint(); fill(to, to + n + 9, -1); vector<int> st; for (int i = 2 * n - 1; i >= 0; i--) { int ind = i % n; while (st.size() and a[st.back() % n] <= a[ind]) st.pop_back(); if (i < n and st.size()) { to[ind] = st.back() % n; } st.push_back(i); } for (int i = 0; i < n; i++) { if (dp[i]) { wyn = max(wyn, dp[i]); continue; } vector<int> prv; int ind = i; while (dp[ind] == 0 and to[ind] != -1) { prv.push_back(ind); ind = to[ind]; } if (to[ind] == -1) dp[ind] = 1; for (int j = prv.size() - 1; j >= 0; j--) { dp[prv[j]] = dp[to[prv[j]]] + 1; } wyn = max(wyn,dp[i]); } putint(wyn); return 0; } |
English