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 INF = 1e9;
pair<int,int> combine(pair<int,int> x, pair<int,int> y)
{
return {min(x.first,y.first),max(x.second,y.second)};
}
pair<int,int> aint[1100000];
int lazy[1100000];
void propagate(int nod)
{
if(!lazy[nod])
return;
lazy[nod*2]+=lazy[nod];
lazy[nod*2+1]+=lazy[nod];
aint[nod*2].first+=lazy[nod];
aint[nod*2].second+=lazy[nod];
aint[nod*2+1].first+=lazy[nod];
aint[nod*2+1].second+=lazy[nod];
lazy[nod]=0;
}
void upd(int nod, int st, int dr, int le, int ri, int newv)
{
if(le>ri)
return;
if(le==st && dr==ri)
{
lazy[nod]+=newv;
aint[nod].first+=newv;
aint[nod].second+=newv;
return;
}
propagate(nod);
int mij=(st+dr)/2;
upd(nod*2,st,mij,le,min(mij,ri),newv);
upd(nod*2+1,mij+1,dr,max(mij+1,le),ri,newv);
aint[nod] = combine(aint[nod*2], aint[nod*2+1]);
}
pair<int,int> qry(int nod, int st, int dr, int le, int ri)
{
if(le>ri)
return {INF,-INF};
if(le==st && dr==ri)
return aint[nod];
propagate(nod);
int mij=(st+dr)/2;
return combine(qry(nod*2,st,mij,le,min(mij,ri)),
qry(nod*2+1,mij+1,dr,max(mij+1,le),ri));
}
bool verif(int le, int ri, int cnt0, int N)
{
pair<int,int> x = qry(1,0,N,0,le-1);
pair<int,int> y = qry(1,0,N,ri,N);
if(x.first > y.first)
swap(x,y);
if(x.second + cnt0 - 1 >= y.first)
return 1;
return 0;
}
vector<int> pozs[500005];
int sequence(int N, std::vector<int> A)
{
for(int i=1;i<=N;i++)
{
upd(1,0,N,i,N,+1);
pozs[A[i-1]].push_back(i);
}
int mxm=1;
for(int val=1;val<=N;val++)
{
for(auto x:pozs[val])
upd(1,0,N,x,N,-1);
int st=0;
for(int i=0;i<pozs[val].size();i++)
{
while(!verif(pozs[val][st],pozs[val][i],i-st+1,N))
st++;
mxm = max(mxm, i-st+1);
}
for(auto x:pozs[val])
upd(1,0,N,x,N,-1);
}
return mxm;
}
Compilation message (stderr)
sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:77:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for(int i=0;i<pozs[val].size();i++)
| ~^~~~~~~~~~~~~~~~~
# | 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... |