Pagini recente » Cod sursa (job #1104780) | Cod sursa (job #18239) | Cod sursa (job #2422043) | Cod sursa (job #37733) | Cod sursa (job #177436)
Cod sursa(job #177436)
#include <stdio.h>
#include <stdlib.h>
#define NX 100010
struct ent {
int val, ix;
};
typedef struct ent ent;
ent t[ NX ];
int N, res, v[ NX ], a[ NX ], c[ NX ], d[ NX ], T[ NX ];
void cit() {
int i;
scanf( "%d", &N );
for( i = 1; i <= N; i++ )
scanf( "%d", &v[i] );
}
void upd( int x, int y ) {
for( ; x <= N; x += x & -x )
if( c[ T[x] ] < c[ y ] )
T[x] = y;
}
int query( int x ) {
int res = 0;
for( ; x; x -= x & -x )
if( c[ T[x] ] > c[ res ] )
res = T[x];
return res;
}
int cmp( const void* x, const void *y ) {
return ((ent *)x)->val - ((ent *)y)->val;
}
void rez() {
int i;
for( i= 1; i <= N; i++ )
t[i].val = v[i], t[i].ix = i;
//normalize
qsort( t+1, N, sizeof( ent ), cmp );
t[0].val = -1;
for( i = 1; i <= N; i++ )
a[ t[i].ix ] = (t[i].val == t[i-1].val) ? a[ t[i-1].ix ] : a[ t[i-1].ix ] + 1;
for( i = 1; i <= N; i++ ) {
d[i] = query( a[i]-1 );
c[i] = c[ d[i] ] + 1;
upd( a[i], i );
}
for( i = 1, res = 0; i <= N; i++ )
res = c[res] < c[i] ? i : res;
}
void baga( int x ) {
if( x > 0 ) {
baga( d[ x ] );
printf( "%d ", v[ x ] );
}
}
void scr() {
printf( "%d\n", c[ res ] );
baga( res );
}
int main() {
freopen( "scmax.in", "r", stdin );
freopen( "scmax.out", "w", stdout );
cit();
rez();
scr();
return 0;
}