Pagini recente » Cod sursa (job #2926058) | Cod sursa (job #3211601) | Cod sursa (job #533325) | Cod sursa (job #552662) | Cod sursa (job #1339072)
#include <fstream>
#define NMAX 100004
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n;
int a[NMAX];
int best[NMAX];
int poz[NMAX];
int lgmax, nr;
void init();
void scm();
void afisare();
int cautare_binara(int);
int main(){
init();
scm();
afisare();
return 0;
}
void init(){
fin>>n;
int i;
for(i = 1; i<= n; ++i) fin>>a[i];
}
void scm(){
int i,aux;
lgmax = 1;
best[1] = a[1];
poz[1] = 1;
for(i = 2; i<= n; ++i){
if(a[i] > best[lgmax]){
best[++lgmax] = a[i];
poz[i] = lgmax;
} else if( a[i] < best[lgmax]){
aux =cautare_binara(i);
best[aux] = a[i];
poz[i] = aux;
}
}
}
int cautare_binara(int x){
int st, dr, m;
st = 1; dr = lgmax;
while(st < dr){
m = (st+dr)/2;
if(best[m] > a[x] ) dr = m;
else st = m+1;
}
return dr;
}
void afisare(){
fout<<lgmax<<'\n';
int i;
int St[NMAX], vf = 0;
for(i = n; i>= 1; --i)
if( poz[i] == lgmax ){
St[++vf] = a[i];
--lgmax;
}
while(vf >= 2){
fout<<St[vf] <<" ";
--vf;
}
fout<<St[vf]<<'\n';
}