#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 5e5;
int N, A[MAXN+10];
vector<int> V[MAXN+10];
struct Node
{
int sum, minv, maxv;
Node() : sum(0), minv(0), maxv(0) {}
Node(int x) : sum(x), minv(min(x, 0)), maxv(max(x, 0)) {}
};
Node operator + (const Node &p, const Node &q)
{
Node ret;
ret.sum=p.sum+q.sum;
ret.minv=min(p.minv, p.sum+q.minv);
ret.maxv=max(p.maxv, p.sum+q.maxv);
return ret;
}
struct SEG
{
Node tree[MAXN*4+10];
void update(int node, int tl, int tr, int p, int q)
{
if(p<tl || tr<p) return;
if(tl==tr)
{
tree[node]=Node(q);
return;
}
int mid=tl+tr>>1;
if(p<=mid) update(node*2, tl, mid, p, q);
else update(node*2+1, mid+1, tr, p, q);
tree[node]=tree[node*2]+tree[node*2+1];
}
Node query(int node, int tl, int tr, int l, int r)
{
if(l<=tl && tr<=r) return tree[node];
if(r<tl || tr<l) return Node();
int mid=tl+tr>>1;
return query(node*2, tl, mid, l, r)+query(node*2+1, mid+1, tr, l, r);
}
}seg;
//int sequence(int _N, std::vector<int> _A)
int main()
{
//N=_N;
cin >> N;
for(int i=1; i<=N; i++) cin >> A[i];
for(int i=1; i<=N; i++) V[i].push_back(0);
for(int i=1; i<=N; i++) V[A[i]].push_back(i);
for(int i=1; i<=N; i++) V[i].push_back(N+1);
for(int i=1; i<=N; i++) seg.update(1, 1, N, i, 1);
int ans=0;
for(int i=1; i<=N; i++)
{
for(auto it : V[i]) seg.update(1, 1, N, it, 0);
for(auto it : V[i-1]) seg.update(1, 1, N, it, -1);
vector<pii> VV;
int t=0;
for(int j=1; j<V[i].size(); j++)
{
Node nd=seg.query(1, 1, N, V[i][j-1]+1, V[i][j]-1);
VV.push_back({t+nd.minv, t+nd.maxv});
t+=nd.sum;
}
//for(auto it : VV) printf("%d %d\n", it.first, it.second);
for(int j=0; j<VV.size(); j++)
{
int lo=-1, hi=j;
while(lo+1<hi)
{
int mid=lo+hi>>1;
if(VV[j].second+j<VV[mid].first || VV[j].first-j>VV[mid].second) lo=mid;
else hi=mid;
}
ans=max(ans, j-hi);
VV[j].first+=j; VV[j].second-=j;
if(j)
{
VV[j].first=min(VV[j].first, VV[j-1].first);
VV[j].second=max(VV[j].second, VV[j-1].second);
}
}
//printf("-> %d\n", i);
}
printf("%d", ans);
return 0;
}
Compilation message
sequence.cpp: In member function 'void SEG::update(int, int, int, int, int)':
sequence.cpp:41:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | int mid=tl+tr>>1;
| ~~^~~
sequence.cpp: In member function 'Node SEG::query(int, int, int, int, int)':
sequence.cpp:50:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int mid=tl+tr>>1;
| ~~^~~
sequence.cpp: In function 'int main()':
sequence.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(int j=1; j<V[i].size(); j++)
| ~^~~~~~~~~~~~
sequence.cpp:81:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int j=0; j<VV.size(); j++)
| ~^~~~~~~~~~
sequence.cpp:86:27: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
86 | int mid=lo+hi>>1;
| ~~^~~
/usr/bin/ld: /tmp/cc1NTyrE.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccYsAwSC.o:sequence.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/cc1NTyrE.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