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>
using namespace std;
const int LIM=2e3+10;
vector<vector<int>> pre;
int ans=1;
const int MN=5e5+10;
int mx_e=4;
int a[MN];
int n;
map<int,vector<int>> ind;
void compute_pre()
{
// LIM x LIM
pre.assign(LIM,vector<int>(LIM));
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);
}
}
}
void adv_pre()
{
// MN x 4
pre.assign(MN,vector<int>(6));
for(int i=1;i<=3;i++)
pre[0][i]=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=3;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);
// 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 s=0;
int e=mx_e;
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=mx_e;
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 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();
// mx_e=n+1;
// int ans=1;
// for(int l=0;l<n;l++)
// {
// for(int r=l+1;r<n;r++)
// {
// ans=max(ans,solve_(l,r));
// }
// }
// return ans;
// // cout<<ans<<endl;
// }
// else{
adv_pre();
for(int i=0;i<n;i++)
ind[a[i]].push_back(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]));
}
}
}
return ans;
// }
return 0;
}
Compilation message (stderr)
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:132:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | for(int i=0;i<ind[x].size();i++)
| ~^~~~~~~~~~~~~~
sequence.cpp:134:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | 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... |