#include <bits/stdc++.h>
#define ssize(x) int(x.size())
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define V vector
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int mod = 119<<23|1;
ll infll = 2e18;
int add(int a, int b) { return a+b >= mod ? a+b-mod : a+b; }
int sub(int a, int b) { return add(mod-b, a); }
int mul(int a, int b) { return int(a*ll(b)%mod); }
int fpow(int a, int b = mod-2) {
int res = 1;
while(b) {
if (b & 1) res = mul(res, a);
b >>= 1, a = mul(a, a);
}
return res;
}
int divd(int a, int b) {
assert(b != 0);
return mul(a, fpow(b));
}
template<typename... Args>
void read(Args&... args) {
auto read_one = [&](auto &a) {
a = 0; int c = getchar_unlocked();
while (c < '0' || '9' < c) c = getchar_unlocked();
while ('0' <= c && c <= '9') a = a*10+c-'0', c = getchar_unlocked();
};
return (read_one(args), ...);
}
void answer() {
int n; scanf("%d", &n);
V<int> t(2*n+1);
for (int i = 1; i <= n; ++i) scanf("%d", &t[i]), t[n+i] = t[i];
V<int> nxt(2*n+1, 0);
V<pii> st;
for (int i = 1; i <= 2*n; ++i) {
while (ssize(st) && st.back().first < t[i])
nxt[st.back().second] = i, st.pop_back();
st.emplace_back(t[i], i);
}
struct dsu {
V<int> rep, sz, res, depth;
dsu(int n, V<int> &nxt) {
rep.resize(n+1); iota(all(rep), 0);
sz.resize(n+1, 1);
res.resize(n+1, 0); iota(all(res), 0);
depth.resize(n+1, 0);
for (int i = n; i >= 1; --i) {
if (!nxt[i]) depth[i] = 1;
else depth[i] = depth[nxt[i]]+1;
}
}
int Find(int x) {
if (rep[x] != x) rep[x] = Find(rep[x]);
return rep[x];
}
void Union(int a, int b) {
// printf("%d %d: %d %d\n", a, b, col[a], col[b]);
a = Find(a), b = Find(b);
if (a == b) return;
if (sz[a] < sz[b]) swap(a, b);
rep[b] = a, sz[a] += sz[b];
if (depth[a] > depth[b])
depth[a] = depth[b], res[a] = res[b];
}
} dsu(2*n, nxt);
// for (int i = 1; i <= 2*n; ++i) printf("%d ", nxt[i]);
// printf("\n");
// for (int i = 1; i <= 2*n; ++i) printf("%d ", dsu.depth[i]);
// printf("\n");
int result = 0;
V<int> path;
for (int i = 1; i <= 2*n; ++i) {
path.clear();
int x = dsu.res[dsu.Find(i)];
path.emplace_back(x);
while (nxt[x] && dsu.res[dsu.Find(nxt[x])] < i+n)
x = dsu.res[dsu.Find(nxt[x])], path.emplace_back(x);
// printf("%d %d: %d %d\n", i, x, dsu.depth[i], dsu.depth[x]);
while (ssize(path))
dsu.Union(x, path.back()), path.pop_back();
result = max(result, dsu.depth[i]-dsu.depth[x]+1);
}
printf("%d\n", result);
}
int main() {
int T = 1; //scanf("%d", &T);
for (++T; --T; ) answer();
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | #include <bits/stdc++.h> #define ssize(x) int(x.size()) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define V vector using namespace std; typedef long long ll; typedef pair<int, int> pii; int mod = 119<<23|1; ll infll = 2e18; int add(int a, int b) { return a+b >= mod ? a+b-mod : a+b; } int sub(int a, int b) { return add(mod-b, a); } int mul(int a, int b) { return int(a*ll(b)%mod); } int fpow(int a, int b = mod-2) { int res = 1; while(b) { if (b & 1) res = mul(res, a); b >>= 1, a = mul(a, a); } return res; } int divd(int a, int b) { assert(b != 0); return mul(a, fpow(b)); } template<typename... Args> void read(Args&... args) { auto read_one = [&](auto &a) { a = 0; int c = getchar_unlocked(); while (c < '0' || '9' < c) c = getchar_unlocked(); while ('0' <= c && c <= '9') a = a*10+c-'0', c = getchar_unlocked(); }; return (read_one(args), ...); } void answer() { int n; scanf("%d", &n); V<int> t(2*n+1); for (int i = 1; i <= n; ++i) scanf("%d", &t[i]), t[n+i] = t[i]; V<int> nxt(2*n+1, 0); V<pii> st; for (int i = 1; i <= 2*n; ++i) { while (ssize(st) && st.back().first < t[i]) nxt[st.back().second] = i, st.pop_back(); st.emplace_back(t[i], i); } struct dsu { V<int> rep, sz, res, depth; dsu(int n, V<int> &nxt) { rep.resize(n+1); iota(all(rep), 0); sz.resize(n+1, 1); res.resize(n+1, 0); iota(all(res), 0); depth.resize(n+1, 0); for (int i = n; i >= 1; --i) { if (!nxt[i]) depth[i] = 1; else depth[i] = depth[nxt[i]]+1; } } int Find(int x) { if (rep[x] != x) rep[x] = Find(rep[x]); return rep[x]; } void Union(int a, int b) { // printf("%d %d: %d %d\n", a, b, col[a], col[b]); a = Find(a), b = Find(b); if (a == b) return; if (sz[a] < sz[b]) swap(a, b); rep[b] = a, sz[a] += sz[b]; if (depth[a] > depth[b]) depth[a] = depth[b], res[a] = res[b]; } } dsu(2*n, nxt); // for (int i = 1; i <= 2*n; ++i) printf("%d ", nxt[i]); // printf("\n"); // for (int i = 1; i <= 2*n; ++i) printf("%d ", dsu.depth[i]); // printf("\n"); int result = 0; V<int> path; for (int i = 1; i <= 2*n; ++i) { path.clear(); int x = dsu.res[dsu.Find(i)]; path.emplace_back(x); while (nxt[x] && dsu.res[dsu.Find(nxt[x])] < i+n) x = dsu.res[dsu.Find(nxt[x])], path.emplace_back(x); // printf("%d %d: %d %d\n", i, x, dsu.depth[i], dsu.depth[x]); while (ssize(path)) dsu.Union(x, path.back()), path.pop_back(); result = max(result, dsu.depth[i]-dsu.depth[x]+1); } printf("%d\n", result); } int main() { int T = 1; //scanf("%d", &T); for (++T; --T; ) answer(); return 0; } |
English