Cod sursa(job #406485)

Utilizator ssergiussSergiu-Ioan Ungur ssergiuss Data 1 martie 2010 16:13:57
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.95 kb
#include <algorithm>
using namespace std;

#define DIM 100005
#define MAX 10005

char s[ MAX ];
int N, XXX, A[ DIM ];
int id_xxx, cnt_s, ind_aux, arb[ DIM<<2 ], ant[ DIM ], sol[ DIM ], id_1[ DIM ], id_2[ DIM ];

struct cmp {

    bool operator()( const int &x, const int &y ) {

        return A[ x ] < A[ y ];
    }
};

void query( const int &nod, const int &st, const int &dr, const int &x, const int &y ) {

    int mij;

    if( x <= st && dr <= y ) {

        if( sol[ arb[ nod ] ] > sol[ ind_aux ] )
            ind_aux = arb[ nod ];

        return;
    }

    mij = (st+dr) / 2;
    if( x <= mij )
        query( nod<<1, st, mij, x, y );
    if( mij < y )
        query( (nod<<1) + 1, mij+1, dr, x, y );
}

void update( const int &nod, const int &st, const int &dr, const int &poz, const int &ind ) {

    int mij;

    if( st == dr ) {

        if( sol[ ind ] > sol[ arb[ nod ] ] )
            arb[ nod ] = ind;

        return;
    }

    mij = (st+dr) / 2;
    if( poz <= mij )
        update( nod<<1, st, mij, poz, ind );
    else
        update( (nod<<1) + 1, mij+1, dr, poz, ind );

    if( sol[ arb[ nod<<1 ] ] > sol[ arb[ (nod<<1) + 1 ] ] )
        arb[ nod ] = arb[ nod<<1 ];
    else
        arb[ nod ] = arb[ (nod<<1) + 1 ];
}

void print( const int &ind ) {

    if( ant[ ind ] )
        print( ant[ ind ] );

    printf( "%d ", A[ ind ] );
}

void read( int &val ) {

    while( !isdigit( s[ cnt_s ] ) )
        if( ++cnt_s == MAX ) {

            fread( s, 1, MAX, stdin );
            cnt_s = 0;
        }

    val = 0;
    while( isdigit( s[ cnt_s ] ) ) {

        val = val*10 + s[ cnt_s ] - '0';
        if( ++cnt_s == MAX ) {

            fread( s, 1, MAX, stdin );
            cnt_s = 0;
        }
    }
}

int main() {

    freopen( "scmax.in", "r", stdin );
    freopen( "scmax.out", "w", stdout );

    int i, cnt_1, cnt_2;

    cnt_s = MAX-1;

    read( N );
    for( i = 1; i <= N; ++i ) {

        read( A[ i ] );
        id_1[ i ] = i;
    }

    sort( id_1 + 1, id_1 + N+1, cmp() );

//    for( int i = 1; i <= N; ++i )
//        printf( "%d\n", id_1[ i ] );

    cnt_1 = cnt_2 = 0;
    for( i = 1; i <= N; ++i ) {

        if( A[ id_1[ i ] ] != A[ id_1[ i-1 ] ] ) {

            cnt_1 += cnt_2 + 1;
            cnt_2 = 0;
        }
        else
            ++cnt_2;

        id_2[ id_1[ i ] ] = cnt_1;
    }

//    for( int i = 1; i <= N; ++i )
//        printf( "%d\n", id_2[ i ] );

//    for( i = 1; i <= N; ++i ) {
//
//        ind_aux = 0;
//        if( id_2[ i ] > 1 )
//            query( 1, 1, N, 1, id_2[ i ] - 1 );
//
//        sol[ i ] = sol[ ind_aux ] + 1;
//        ant[ i ] = ind_aux;
//        update( 1, 1, N, id_2[ i ], i );
//
//        if( sol[ i ] > XXX ) {
//
//            XXX = sol[ i ];
//            id_xxx = i;
//        }
//    }
//
//    printf( "%d\n", XXX );
//    print( id_xxx );

    return 0;
}