#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cstdlib>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
int maxi = 0;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++){
cin >> a[i];
maxi = max(maxi, a[i]);
}
vector<int> V(n, -1);
vector<int> st;
st.reserve(2 * n);
for (int i = 2 * n - 1; i >= 0; i--){
int x = a[i % n];
while (!st.empty() && a[st.back() % n] <= x){
st.pop_back();
}
if (i < n) {
if (!st.empty()){
V[i] = st.back() % n;
}
}
st.push_back(i);
}
vector<int> dp(n, 0);
vector<int> head(maxi+1, -1);
vector<int> buck(n, -1);
for (int i = 0; i < n; i++){
buck[i] = head[a[i]];
head[a[i]] = i;
}
int out = 0;
for (int i=maxi; i>=1; i--){
int it = head[i];
while (it != -1){
if (V[it] == -1){
dp[it] = 1;
} else {
dp[it] = 1 + dp[V[it]];
}
out = max(out, dp[it]);
it = buck[it];
}
}
cout << out;
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 | #include <iostream> #include <string> #include <algorithm> #include <vector> #include <cstdlib> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; int maxi = 0; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++){ cin >> a[i]; maxi = max(maxi, a[i]); } vector<int> V(n, -1); vector<int> st; st.reserve(2 * n); for (int i = 2 * n - 1; i >= 0; i--){ int x = a[i % n]; while (!st.empty() && a[st.back() % n] <= x){ st.pop_back(); } if (i < n) { if (!st.empty()){ V[i] = st.back() % n; } } st.push_back(i); } vector<int> dp(n, 0); vector<int> head(maxi+1, -1); vector<int> buck(n, -1); for (int i = 0; i < n; i++){ buck[i] = head[a[i]]; head[a[i]] = i; } int out = 0; for (int i=maxi; i>=1; i--){ int it = head[i]; while (it != -1){ if (V[it] == -1){ dp[it] = 1; } else { dp[it] = 1 + dp[V[it]]; } out = max(out, dp[it]); it = buck[it]; } } cout << out; return 0; } |
English