Cod sursa(job #649401)
#include <fstream>
#include <iostream>
#define maxn 100010
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n,nr_stive,v[maxn],prec[maxn];
struct nod{
int inf;
int prec;
};
int stiva[maxn];
int binsearch(int x, int st, int dr)
{
if (dr==0) return -1;
if (dr==st)
if (v[stiva[st]]>=x) return st;
else return -1;
if ((dr-st)==1){
if (v[stiva[st]]>=x) return st;
if (v[stiva[dr]]>=x) return dr;
return -1;
}
int m=(dr+st)/2;
if (v[stiva[m]]>=x)
if (v[stiva[m-1]]>=x) return binsearch(x,st,m-1);
else return m;
else return binsearch(x,m+1,dr);
}
void patience()
{
fin>>n;
nr_stive=0;
for (int i=1; i<=n; i++){
fin>>v[i];
// fout<<v[i]<<endl;
int j=binsearch(v[i],1,nr_stive);
// fout<<"* "<<j<<" stiva[j]= "<<v[stiva[j]]<<" stive="<<nr_stive<<" i="<<v[i]<<endl;
if (j==-1){
stiva[++nr_stive]=i;
// fout<<"* "<<j<<" stiva[j]= "<<v[stiva[j]]<<" stive="<<nr_stive<<" i="<<v[i]<<endl;
if (nr_stive>1) prec[i]=stiva[nr_stive-1];
// fout<<"#####"<<endl;
}
else{
stiva[j]=i;
if (j>1) prec[i]=stiva[j-1];
}
// fout<<"***"<<endl;
}
}
void afisare(int x)
{
if (prec[x]!=0) afisare(prec[x]);
fout<<v[x]<<" ";
}
int main()
{
patience();
int k=stiva[nr_stive];
fout<<nr_stive<<endl;
// for (int j=1;j<=nr_stive;j++)
// {fout<<k<<" ";
// k=prec[k];
// }
afisare(k);
return 0;
}