Pagini recente » Cod sursa (job #1266901) | Cod sursa (job #2705776) | Cod sursa (job #2762199) | Cod sursa (job #1932818) | Cod sursa (job #875597)
Cod sursa(job #875597)
#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, where;
std::vector<unsigned> tati(N);
std::vector<unsigned> MinIndL(N+1);
MinIndL[0]=0;
Lmax=1; where=0; tati[0]=N; MinIndL[1]=0;
for(unsigned i=1;i<N;++i){
unsigned hi=Lmax, lo=0, j=0;
while(hi>lo){
j=hi-(hi-lo)/2;
if(a[MinIndL[j]]>=a[i]){ hi=j-1; j=0; }
else lo=j;
}
if(j==0) tati[i]=N; else tati[i]=MinIndL[j];
if(j==Lmax){
Lmax++; where=i;
MinIndL[Lmax]=i;
}
else if(a[i]<a[MinIndL[j+1]]) MinIndL[j+1]=i;
}
//use MinIndL as temp storage:
unsigned c=0;
for(;where!=N;where=tati[where]) MinIndL[c++]=a[where];
fout<<Lmax<<'\n';
while(c--) fout<<MinIndL[c]<<' ';
fout<<'\n';
}