Submission #985165

#TimeUsernameProblemLanguageResultExecution timeMemory
985165Faisal_SaqibSequence (APIO23_sequence)C++17
28 / 100
161 ms17984 KiB
#include "sequence.h" #include <bits/stdc++.h> // #include "grader.cpp" using namespace std; const int LIM=2e3+10; int pre[LIM][LIM]; int a[LIM],n; 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; } const int MN=5e5+10; int b[MN],l_[MN],r_[MN],eq_[MN]; map<int,vector<int>> ind; 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+1)/2; if(cntp>=cnt_p) { return cntp; } else{ return 0; } } int sequence(int NP, std::vector<int> AP) { if(NP<LIM) { n=NP; for(int j=0;j<n;j++) a[j]=AP[j]; 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{ n=NP; for(int i=0;i<n;i++) b[i]=AP[i]; 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])); return ans; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...