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 "sequence.h"
#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
#include <cstdlib>
#include <cstdint>
using namespace std;
#define ll long long
#define f first
//#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
const int mxn=5e5,lg=24;
int mx=0;
struct fen{
//using fenwick cus i dont wanna write slding median
int fwk[mxn+10];
vector<pii>keep;
void update(int pos,int val){
keep.pb({pos,val});
for(int i=pos;i<=mx;i+=(i&-i))fwk[i]+=val;
}
int qry(int pos){
if(pos<0)return 0;
int sum=0;
for(int i=pos;i>0;i-=(i&-i))sum+=fwk[i];
return sum;
}
void re(){
for(auto i:keep)for(int j=i.f;j<=mx;j+=(j&-j))fwk[j]-=i.s;
keep.clear();
}
int get(int x){
x++;
int pos=0,sum=0;
for(int i=lg;i>=0;i--){
if(pos+(1LL<<i)<=mx&&fwk[pos+(1LL<<i)]+sum<x){
pos+=(1LL<<i);
sum+=fwk[pos];
}
}
return pos+1;
}
}t;
int cnt[mxn+10],r[mxn+10],l[mxn+10];
int sequence(int n, vector<int>v){
int ans=0;
if(n>2e3){
//sub 3 case get l,r or get l-> other side
for(auto i:v)cnt[i]++,l[i]=n+1;
for(int i=0;i<n;i++)r[v[i]]=max(r[v[i]],i),l[v[i]]=min(l[v[i]],i);
for(int i=0;i<n;i++){
int cur=i;
while(cur<v.size()&&v[cur]==v[i])cur++;
ans=max(ans,cur-i);//take range l,r
i=cur-1;
}
for(auto i:v){//take l->r
int other=n-(r[i]-l[i]+1);
if(other+cnt[i]>=(n/2)+1)ans=max(ans,cnt[i]);
}
return ans;
}
for(auto i:v)mx=max(mx,i);
auto bru=[&](int x){
int a=t.get(x);
return t.qry(a)-t.qry(a-1);
};
for(int i=0;i<n;i++){
t.re();
for(int j=i;j<n;j++){
t.update(v[j],1);
int x1=(j-i)/2,x2=ceil((j-i)*1.0/2*1.0);
ans=max({ans,bru(x1),bru(x2)});
}
}
if(ans==0)assert(0);
return ans;
}
/*
int32_t main(){
int n;cin>>n;
vector<int>v(n);
for(int i=0;i<n;i++)cin>>v[i];
cout<<sequence(n,v);
}*/
/*
9
1 1 1 2 2 3 2 2 1
*/
/*
constraint of being a medain
A number of number <x,B number of number >x,C number of number ==x
if C>=A+B
if A+C>=B && C<=A+B
if A+B>=c && B<=A+C
i might be wrong
*/
Compilation message (stderr)
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:79:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | while(cur<v.size()&&v[cur]==v[i])cur++;
| ~~~^~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |