Pagini recente » Cod sursa (job #2450280) | Cod sursa (job #2969998) | Cod sursa (job #1613745) | Cod sursa (job #1496729) | Cod sursa (job #875947)
Cod sursa(job #875947)
#include <fstream>
#include <vector>
int main(){
std::ifstream fin("scmax.in");
std::ofstream fout("scmax.out");
unsigned n; fin>>n;
std::vector<unsigned> a(n);
for(unsigned i=0;i<n;++i) fin>>a[i];
unsigned Lmax;
std::vector<unsigned> M(n+1);
std::vector<unsigned> P(n);
M[1]=0; Lmax=1; P[0]=n;
for(unsigned i=1;i<n;++i){
unsigned lo=0,hi=Lmax,j=0;
while(hi>=lo){
if(hi-lo<2){
if(a[M[hi]]<a[i]){ j=hi; break; }
else if(lo!=0&&a[M[lo]]<a[i]){ j=lo; break; }
else { j=0; break; }
}
else{
j=lo+(hi-lo)/2;
if(a[M[j]]>=a[i]) hi=j-1;
else lo=j;
}
}
P[i]=M[j];
if(j==Lmax){
Lmax++;
M[Lmax]=i;
}
else if(a[i]<a[M[j+1]]) M[j+1]=i;
}
std::vector<unsigned> lis(Lmax);
for(int i=Lmax-1;i>=0;--i){ lis[i]=a[M[Lmax]]; M[Lmax]=P[M[Lmax]]; }
fout<<Lmax<<'\n';
for(unsigned i=0;i<Lmax;++i) fout<<lis[i]<<' ';
fout<<'\n';
}