Pagini recente » Cod sursa (job #711343) | Cod sursa (job #771608) | Cod sursa (job #1060279) | Cod sursa (job #1147305) | Cod sursa (job #649403)
Cod sursa(job #649403)
#include <fstream>
#define maxn 100010
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n,nr_stive,v[maxn],prec[maxn],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];
int j=binsearch(v[i],1,nr_stive);
if (j==-1){
stiva[++nr_stive]=i;
if (nr_stive>1) prec[i]=stiva[nr_stive-1];
}
else{
stiva[j]=i;
if (j>1) prec[i]=stiva[j-1];
}
}
}
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;
afisare(k);
return 0;
}