Cod sursa(job #1345664)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 17 februarie 2015 19:47:15
Problema Subsir crescator maximal Scor 35
Compilator cpp Status done
Runda Arhiva educationala Marime 1.3 kb
#include<fstream>
#include<vector>
#include<algorithm>

using namespace std;

ifstream fin( "scmax.in" );
ofstream fout( "scmax.out" );

const int nmax = 100000;
int val[ nmax + 1 ];
int n, d[ nmax + 1 ], aib[ nmax + 1 ], v[ nmax + 1 ], r[ nmax + 1 ];

void update( int pos, int x ) {
    for( int i = pos; i <= n; i += (i & -i) ) {
        if ( d[ aib[ i ] ] < d[ x ] ) {
            aib[ i ] = x;
        }
    }
}
int query( int val ) {
    int sol = 0;
    for( int i = val; i > 0; i -= (i & -i) ) {
        if ( d[ sol ] < d[ aib[ i ] ] ) {
            sol = aib[ i ];
        }
    }
    return sol;
}
void normalizare() {
    vector <int> aux( val + 1, val + n + 1 );
    sort( aux.begin(), aux.end() );
    aux.erase( unique( aux.begin(), aux.end() ), aux.end() );
    for( int i = 1; i <= n; ++ i ) {
        v[ i ] = lower_bound( aux.begin(), aux.end(), val[ i ] ) - aux.begin() + 1;
    }
}
int main() {
    int ans;
    fin >> n;
    ans = 0;
    for( int i = 1; i <= n; ++ i ) {
        fin >> val[ i ];
    }
    normalizare();
    for( int i = 1; i <= n; ++ i ) {
        r[ i ] = query( v[ i ] - 1 );
        d[ i ] = d[ r[ i ] ] + 1;
        if ( d[ i ] > d[ ans ] ) {
            ans = i;
        }
        update( v[ i ], i );
    }
    fout << d[ ans ] << "\n";
    fin.close();
    fout.close();
    return 0;
}