# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
958118 | zeta7532 | Sequence (APIO23_sequence) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = int;
const ll mod = 998244353;
#define fi first
#define se second
#define rep(i,n) for(ll i=0;i<n;i++)
#define all(x) x.begin(),x.end()
#define faster ios::sync_with_stdio(false);cin.tie(nullptr)
pair<int,int> f(pair<int,int> a,pair<int,int> b){
return {min(a.fi,b.fi),max(a.se,b.se)};
}
template <typename T>
struct RMQ{
const T INF={0,0};
ll N;
vector<T> dat;
vector<int> lazy;
RMQ(ll N_):N(),dat(N_*4,INF),lazy(N_*4,0){
ll x=1;
while(N_>x) x*=2;
N=x;
}
void eval(ll k,ll l,ll r){
if(lazy[k]==0) return;
if(k<N-1){
lazy[k*2+1]+=lazy[k];
lazy[k*2+2]+=lazy[k];
}
dat[k]={dat[k].fi+lazy[k],dat[k].se+lazy[k]};
lazy[k]=0;
}
void update(ll a,ll b,int x,ll k,ll l,ll r){
eval(k,l,r);
if(a<=l&&r<=b){
lazy[k]+=x;
eval(k,l,r);
}else if(a<r&&l<b){
update(a,b,x,k*2+1,l,(l+r)/2);
update(a,b,x,k*2+2,(l+r)/2,r);
dat[k]=f(dat[k*2+1],dat[k*2+2]);
}
}
void update(ll a,ll b,int x){update(a,b,x,0,0,N);}
T query_sub(ll a,ll b,ll k,ll l,ll r){
eval(k,l,r);
if(r<=a||b<=l){
return {1LL<<30,-1LL<<30};
}else if(a<=l&&r<=b){
return dat[k];
}else{
T vl=query_sub(a,b,k*2+1,l,(l+r)/2);
T vr=query_sub(a,b,k*2+2,(l+r)/2,r);
return f(vl,vr);
}
}
T query(ll a,ll b){return query_sub(a,b,0,0,N);}
};
int f(int N,vector<int> A){
vector<int> B=A;
sort(all(B));
B.erase(unique(all(B)),B.end());
vector<vector<int>> G(N+1);
rep(i,N) G[A[i]].push_back(i);
int ans=0;
RMQ<pair<int,int>> seg(N+1);
rep(j,N+1) seg.update(j,j+1,j);
rep(i,B.size()){
int l=0,r=0;
while(1){
if(r+1==G[B[i]].size()) break;
auto e1=seg.query(0,G[B[i]][l]+1);
auto e2=seg.query(G[B[i]][r+1]+1,N+1);
if(e1.fi<=e2.se&&e2.fi<=e1.se){
r++;
}else{
l++;
}
if(r<l) r++;
ans=max(ans,r-l+1);
}
for(int j:G[B[i]]){
seg.update(j+1,N+1,-2);
}
}
return ans;
}
int sequence(int N, std::vector<int> A) {
int ans=0;
vector<int> B=A;
sort(all(B));
rep(j,2){
int cnt=0;
rep(i,N) if(A[i]==B[(N-1+j)/2]) cnt++;
ans=max(ans,cnt);
}
ans=max(ans,f(N,A));
rep(i,N) A[i]=N+1-A[i];
ans=max(ans,f(N,A));
return ans;
}
int main() {
int N;
assert(1 == scanf("%d", &N));
std::vector<int> A(N);
for (int i = 0; i < N; ++i) {
assert(1 == scanf("%d", &A[i]));
}
int result = sequence(N, A);
printf("%d\n", result);
return 0;
}