Pagini recente » Cod sursa (job #207322) | Cod sursa (job #2682813) | Cod sursa (job #367942) | Cod sursa (job #2949067) | Cod sursa (job #203915)
Cod sursa(job #203915)
#include <cstdlib>
#include <algorithm>
#define nmax 100002
using namespace std;
int n,a[nmax],x[nmax],res[nmax],d[nmax],arb[nmax],miau[nmax];
int i,aux,best;
void update(int x, int y);
int query(int 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]),
res[i] = x[i] = a[i];
sort(x+1,x+n+1);
for(i=2,aux=1; i<=n; ++i)
if(x[i] != x[aux])
x[++aux] = x[i];
for(i=1; i<=n; ++i)
a[i] = lower_bound(x+1,x+aux+1, a[i])-x;
for(i=1; i<=n; ++i){
miau[i] = query(a[i] - 1);
d[i] = d[miau[i]] + 1;
update(a[i],i);
}
for(i=1; i<=n; ++i)
if(d[i]>d[best])
best = i;
printf("%d\n",d[best]);
for(aux = 0, i = best; i; i = miau[i])
x[++aux] = res[i];
for(i=aux; i; --i)
printf("%d ",x[i]);
printf("\n");
return 0;
}
void update(int x, int y){
for(; x<=n; x+= x^(x-1)&x)
if(d[arb[x]] < d[y])
arb[x] = y;
}
int query(int x){
for(aux=0; x; x-=x^(x-1)&x)
if(d[aux] < d[arb[x]])
aux = arb[x];
return aux;
}