Cod sursa(job #1339072)

Utilizator Andrei_TirpescuAndrei Tirpescu Andrei_Tirpescu Data 10 februarie 2015 17:39:09
Problema Subsir crescator maximal Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>
#define NMAX 100004
using namespace std;

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

int n;
int a[NMAX];

int best[NMAX];
int poz[NMAX];
int lgmax, nr;

void init();
void scm();
void afisare();
int cautare_binara(int);

int main(){

    init();
    scm();
    afisare();

    return 0;
}

void init(){
    fin>>n;
    int i;
    for(i = 1; i<= n; ++i) fin>>a[i];
}

void scm(){
    int i,aux;
    lgmax = 1;
    best[1] = a[1];
    poz[1] = 1;


    for(i = 2; i<= n; ++i){
        if(a[i] > best[lgmax]){
            best[++lgmax] = a[i];
            poz[i] = lgmax;
        } else if( a[i] < best[lgmax]){
            aux =cautare_binara(i);
            best[aux] = a[i];
            poz[i] = aux;
        }
    }
}

int cautare_binara(int x){
    int st, dr, m;
    st = 1; dr = lgmax;
    while(st < dr){
        m = (st+dr)/2;
        if(best[m] > a[x] ) dr = m;
        else st = m+1;
    }
    return dr;
}

void afisare(){
    fout<<lgmax<<'\n';

    int i;
    int St[NMAX], vf = 0;

    for(i = n; i>= 1; --i)
        if( poz[i] == lgmax ){
            St[++vf] = a[i];
            --lgmax;
        }
    while(vf >= 2){
        fout<<St[vf] <<" ";
        --vf;
    }
    fout<<St[vf]<<'\n';
}