Cod sursa(job #292242)
Utilizator | Todor Alex lexu93 | Data | 30 martie 2009 21:47:12 |
---|---|---|---|
Problema | Subsir crescator maximal | Scor | 100 |
Compilator | cpp | Status | done |
Runda | The Beginners | Marime | 1.27 kb |
#include<stdio.h>
#include<string.h>
long n,num,lmax,a[100001],p[100001],q[100001];
long binary_search(long x)
{
long st,dr,m;
st=1;
dr=num;
while(st<=dr)
{
m=(st+dr)/2;
if(q[m]==x)
return m;
if(q[m]<x)
st=m+1;
else
dr=m-1;
}
return st;
}
int main()
{
freopen("scmax.in","r",stdin);
freopen("scmax.out","w",stdout);
long i,poz,k=1;
scanf("%ld",&n);
for(i=1;i<=n;++i)
scanf("%ld",&a[i]);
q[1]=a[1];
p[1]=1;
num=1;
for(i=2;i<=n;++i)
{
poz=binary_search(a[i]);
if(q[poz]==0)
num++;
q[poz]=a[i];
p[++k]=poz;
if(poz>lmax)
lmax=poz;
}
printf("%ld\n",lmax);
memset(q,0,sizeof(q));
int l=0;
for(i=k;i>=1;--i)
{
if(p[i]==lmax)
{
q[++l]=a[i];
lmax--;
}
}
for(i=l;i>=1;--i)
printf("%ld ",q[i]);
printf("\n");
return 0;
}