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 <bits/stdc++.h>
// #include "grader.cpp"
using namespace std;
const int LIM=2e3+10;
int pre[LIM][LIM];
const int MN=5e5+10;
int a[MN],l_[MN],r_[MN],eq_[MN];
int n;
map<int,vector<int>> ind;
void compute_pre()
{
for(int i=1;i<=n;i++)
pre[0][i]=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
pre[i][j]=pre[i-1][j]+(a[i-1]<=j);
}
}
}
int count_less(int l,int r,int x)
{
return pre[r+1][x]-pre[l][x];
}
int count(int l,int r,int x)
{
int tp=count_less(l,r,x)-count_less(l,r,x-1);
// if(tp==4)
// cout<<"FOR "<<l<<' '<<r<<' '<<x<<endl;
return tp;
}
int count_(int l,int r,int x)
{
auto it=lower_bound(begin(ind[x]),end(ind[x]),l);
auto it1=upper_bound(begin(ind[x]),end(ind[x]),r);
return it1-it;
}
int solve_(int l,int r,int x)
{
int cntp=count_(l,r,x);
int len=(r-l+1);
int cnt_p=len/2;
if(cntp>=cnt_p)
{
return cntp;
}
else{
return 0;
}
}
int sequence(int NP, std::vector<int> AP) {
n=NP;
for(int i=0;i<n;i++)
a[i]=AP[i];
if(NP<LIM)
{
compute_pre();
int ans=1;
for(int l=0;l<n;l++)
{
for(int r=l+1;r<n;r++)
{
int s=0;
int e=n+1;
int len=(r-l+1);
int flooor=(len-1)/2;
/*
if len is even
then there are two medain = floor((len-1)/2) and ceil((len-1)/2)
else if len is odd
single medain
*/
while(s+1<e)
{
int mid=(s+e)/2;
if(count_less(l,r,mid)<=flooor)
{
s=mid;
}
else{
e=mid;
}
}
ans=max(ans,count(l,r,e));
if(len%2==0)
{
int ceill=flooor+1;
s=0;
e=n+1;
while(s+1<e)
{
int mid=(s+e)/2;
if(count_less(l,r,mid)<=ceill)
{
s=mid;
}
else{
e=mid;
}
}
ans=max(ans,count(l,r,e));
}
}
}
return ans;
// cout<<ans<<endl;
}
else{
for(int i=0;i<=n;i++)
l_[i]=r_[i]=-1;
for(int i=0;i<n;i++)
{
if(l_[a[i]]==-1)
l_[a[i]]=i;
r_[a[i]]=i;
}
int ans=1;
for(int i=0;i<n;i++)
ind[a[i]].push_back(i);
for(int i=0;i<n;i++)
ans=max(ans,solve_(l_[a[i]],r_[a[i]],a[i]));
set<int> asp;
for(int i=0;i<n;i++)
{
asp.insert(a[i]);
}
for(auto x:asp)
{
for(int i=0;i<ind[x].size();i++)
{
for(int j=(i+1);j<ind[x].size();j++)
{
ans=max(ans,solve_(ind[x][i],ind[x][j],x));
}
}
}
return ans;
}
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:135:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
135 | for(int i=0;i<ind[x].size();i++)
| ~^~~~~~~~~~~~~~
sequence.cpp:137:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
137 | for(int j=(i+1);j<ind[x].size();j++)
| ~^~~~~~~~~~~~~~
# | 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... |