#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int N = 5e5 + 5;
struct node {
int sum = 0;
int suf_max = 0, pref_max = 0;
int suf_min = 0, pref_min = 0;
};
node comb(node lx, node rx) {
node ret;
ret.sum = lx.sum + rx.sum;
ret.suf_max = max(rx.suf_max, rx.sum + lx.suf_max);
ret.pref_max = max(lx.pref_max, lx.sum + rx.pref_max);
ret.suf_min = min(rx.suf_min, rx.sum + lx.suf_min);
ret.pref_min = min(lx.pref_min, lx.sum + rx.pref_min);
return ret;
}
node init(int v) {
node ret;
ret.sum = v;
if (v == 1) {
ret.suf_max = ret.pref_max = 1;
} else {
ret.suf_min = ret.pref_min = -1;
}
return ret;
}
node seg[N * 2];
void upd(int nd, int l, int r, int p, int v) {
if (l == r) {
seg[nd] = init(v);
return;
}
int mid = (l + r) >> 1;
int rs = nd + ((mid - l + 1) << 1);
if (p <= mid) {
upd(nd + 1, l, mid, p, v);
} else {
upd(rs, mid + 1, r, p, v);
}
seg[nd] = comb(seg[nd + 1], seg[rs]);
}
node qry(int nd, int l, int r, int s, int e) {
if (l >= s && r <= e) {
return seg[nd];
}
int mid = (l + r) >> 1;
int rs = nd + ((mid - l + 1) << 1);
if (mid >= e) {
return qry(nd + 1, l, mid, s, e);
} else {
if (mid < s) {
return qry(rs, mid + 1, r, s, e);
} else {
return comb(qry(nd + 1, l, mid, s, e), qry(rs, mid + 1, r, s, e));
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
vector<vector<int>> group(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
--a[i];
group[a[i]].push_back(i);
}
for (int i = 0; i < n; ++i) {
upd(0, 0, n - 1, i, -1);
}
int ans = 0;
for (int x = 0; x < n; ++x) {
for (int i : group[x]) {
upd(0, 0, n - 1, i, 0);
}
for (int i = 0; i < (int) group[x].size(); ++i) {
int l = 0, r = i;
while (l < r) {
bool ok = true;
int mid = (l + r) / 2;
int sum = qry(0, 0, n - 1, group[x][mid], group[x][i]).sum;
int cc = i - mid + 1;
if (sum < 0) {
if (i < n - 1) {
cc += qry(0, 0, n - 1, i + 1, n - 1).pref_max;
}
if (i > 0) {
cc += qry(0, 0, n - 1, 0, i - 1).suf_max;
}
ok &= (sum + cc >= 0);
} else if (sum > 0) {
if (i < n - 1) {
cc += qry(0, 0, n - 1, i + 1, n - 1).pref_min;
}
if (i > 0) {
cc += qry(0, 0, n - 1, 0, i - 1).suf_min;
}
ok &= (sum - cc <= 0);
}
(ok ? r = mid : l = mid + 1);
}
ans = max(ans, i - l + 1);
}
}
cout << ans << '\n';
return 0;
}
Compilation message
/usr/bin/ld: /tmp/ccN65y74.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccllnl65.o:sequence.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccN65y74.o: in function `main':
grader.cpp:(.text.startup+0x2a4): undefined reference to `sequence(int, std::vector<int, std::allocator<int> >)'
collect2: error: ld returned 1 exit status