Pagini recente » Cod sursa (job #645408) | Cod sursa (job #2316723) | Cod sursa (job #2937322) | Cod sursa (job #537357) | Cod sursa (job #535116)
Cod sursa(job #535116)
#include<stdio.h>
#include<string.h>
int a[100002],p[100002],max,aa[100002],tt,q[100002],i,j,t,n,m2,o,l;
bool ok;
int cautareb(int l,int r, int x){
int m;
if (l>r) return(-1);
m=l+r;
m=m/2;
if (q[m]==x) return(m);
if (m==1 && q[m]>x) return(m);
if (q[m]>x && q[m-1]<x) return(m);
if (q[m]>x) return(cautareb(l,m-1,x));
else return(cautareb(m+1,r,x));
}
int main(){
freopen("scmax.in","r",stdin);
freopen("scmax.out","w",stdout);
scanf("%d",&n);
for (i=1; i<=n; i++) scanf("%d",&a[i]);
memset(p,sizeof(p),0);
memset(q,sizeof(q),0);
p[1]=1;
q[1]=a[1];
o=1;
for (i=2; i<=n; i++){
m2=cautareb(1,o,a[i]);
if (m2!=-1){
q[m2]=a[i];
p[i]=m2;
if (p[i]>max) max=p[i];
}
else{
p[i]=p[i-1]+1;
o++;
if (p[i]>max) max=p[i];
q[p[i]]=a[i];
}
t=m2;
m2=-1;
}
printf("%d\n",max);
o=max;
ok=true;
tt=0;
for(i=n; i>=1; i--){
if (p[i]==o) ok=true;
if (p[i]==o && ok==true){
tt++;
aa[tt]=a[i];
o--;
ok=false;
}
}
for (i=tt; i>=1; i--) printf("%d ",aa[i]);
return(0);
}