Pagini recente » Cod sursa (job #597211) | Cod sursa (job #3334247) | Cod sursa (job #3302471) | Cod sursa (job #3358615) | Cod sursa (job #888422)
Cod sursa(job #888422)
#include <fstream>
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
#define LE 100666
int i,j,K,a[LE],best[LE],last[LE];
int n,poz,max_p,l;
int sir[LE];
int bs(int V){
int step,pos;
for(step=1;step<=max_p;step*=2);
for(pos=0;step;step/=2)
if (pos+step<=max_p&&a[best[pos+step]]<=V)
pos+=step;
return pos;
}
int main()
{
f>>n;
for(i=1;i<=n;++i){
f>>a[i];
poz=bs(a[i]-1);
last[i]=best[poz];
if (poz==max_p) {
max_p++,l=i;
best[poz+1]=i;
}
else
if (a[best[poz+1]]>a[i]) best[poz+1]=i;
}
for(K=max_p; l;l=last[l]) sir[K--]=l;
g<<max_p<<'\n';
for(i=1;i<=max_p;++i) g<<a[sir[i]]<<" ";
f.close();
g.close();
return 0;
}