#include <bits/stdc++.h>
#define rep(i, p, k) for (int i = (p); i < (k); ++i)
#define all(v) (v).begin(), (v).end()
#define ll long long
#define fi first
#define sc second
#ifndef DEBUG
#define debug(...)
#else
#include "debug.h"
#endif
using namespace std;
const int L = 21;
const int inf = 1e9;
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
vector<int> t(n);
for (auto &v : t)
cin >> v;
rep(i, 0, n) t.push_back(t[i]);
t.push_back(inf);
vector<vector<int>> nxt(L, vector<int>(2 * n + 1));
map<int, vector<int>> m;
nxt[0][2 * n] = 2 * n;
vector<int> tmp;
rep(i, 0, 2 * n + 1) {
int v = t[i];
tmp.clear();
for (auto &[val, V] : m) {
if (val >= v)
break;
tmp.push_back(val);
for (auto ind : V)
nxt[0][ind] = i;
}
for (auto vv : tmp)
m.erase(vv);
m[v].push_back(i);
}
rep(i, 1, L) rep(j, 0, 2 * n + 1) nxt[i][j] = nxt[i - 1][nxt[i - 1][j]];
int res = 0;
rep(i, 0, n) {
int x = i;
int r = 1;
for (int j = L - 1; j >= 0; --j)
if (nxt[j][x] <= i + n) {
x = nxt[j][x];
r += (1 << j);
}
res = max(res, r);
}
cout << res;
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 | #include <bits/stdc++.h> #define rep(i, p, k) for (int i = (p); i < (k); ++i) #define all(v) (v).begin(), (v).end() #define ll long long #define fi first #define sc second #ifndef DEBUG #define debug(...) #else #include "debug.h" #endif using namespace std; const int L = 21; const int inf = 1e9; int main() { cin.tie(0)->sync_with_stdio(0); int n; cin >> n; vector<int> t(n); for (auto &v : t) cin >> v; rep(i, 0, n) t.push_back(t[i]); t.push_back(inf); vector<vector<int>> nxt(L, vector<int>(2 * n + 1)); map<int, vector<int>> m; nxt[0][2 * n] = 2 * n; vector<int> tmp; rep(i, 0, 2 * n + 1) { int v = t[i]; tmp.clear(); for (auto &[val, V] : m) { if (val >= v) break; tmp.push_back(val); for (auto ind : V) nxt[0][ind] = i; } for (auto vv : tmp) m.erase(vv); m[v].push_back(i); } rep(i, 1, L) rep(j, 0, 2 * n + 1) nxt[i][j] = nxt[i - 1][nxt[i - 1][j]]; int res = 0; rep(i, 0, n) { int x = i; int r = 1; for (int j = L - 1; j >= 0; --j) if (nxt[j][x] <= i + n) { x = nxt[j][x]; r += (1 << j); } res = max(res, r); } cout << res; return 0; } |
English