Cod sursa(job #1338663)

Utilizator Andrei_TirpescuAndrei Tirpescu Andrei_Tirpescu Data 10 februarie 2015 10:43:24
Problema Subsir crescator maximal Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 1.17 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 pb, 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;
    pb = 1;
    best[1] = a[1];
    poz[1] = 1;


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

void afisare(){
    int i;

    nr = poz[1];
    for(i = 2; i<= n; ++i)
        nr = max( poz[i], nr );
    fout<<nr<<'\n';

    for(i = 1; i< pb ; ++i) fout<<best[i]<<' ';
    fout<<best[i]<<'\n';
}

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