Pagini recente » Cod sursa (job #2491247) | Cod sursa (job #448099) | Cod sursa (job #760437) | Cod sursa (job #2896196) | Cod sursa (job #177447)
Cod sursa(job #177447)
#include <cstdio>
#include <vector>
#include <algorithm>
#define NX 100010
#define val first
#define ix second
using namespace std;
pair< int, int > 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;
}
void rez() {
int i;
for( i= 1; i <= N; i++ )
t[i].val = v[i], t[i].ix = i;
//normalize
sort( t+1, t+1+N );
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;
for( i = res, N = 1; i; i = d[i], N++ )
a[ N ] = v[i];
}
void scr() {
int i;
printf( "%d\n", c[ res ] );
for( i = N-1; i; i-- )
printf( "%d ", a[i] );
}
int main() {
freopen( "scmax.in", "r", stdin );
freopen( "scmax.out", "w", stdout );
cit();
rez();
scr();
return 0;
}