Cod sursa(job #2267046)

Utilizator OnofreiTudorOnofrei Tudor OnofreiTudor Data 23 octombrie 2018 10:32:24
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <fstream>

using namespace std;

int a[100001], s[100001], n, poz[100001], best[100001], lgmax;

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

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

int cautbin(int x){
    //caut cel mai mic element >= x in best
    //returnez pozitia lui
    int st = 0, dr = lgmax + 1, mijloc;
    while(dr-st > 1){
        mijloc = (st+dr)/2;
        if(best[mijloc] >= x){
            dr = mijloc;
        }
        else{
            st = mijloc;
        }
    }
    return dr;
}

void construiescbest(){
    int i, pozitie;
    best[1] = a[1]; lgmax = 1; poz[1] = 1;
    for(i = 2; i<=n; i++){
        if(a[i]>best[lgmax]){
            best[++lgmax] = a[i];
            poz[i] = lgmax;
        }
        else{
            pozitie = cautbin(a[i]);
            best[pozitie] = a[i];
            poz[i] = pozitie;
        }
    }
}

void afisare(){
    int cine, i;
    fout << lgmax << '\n';
    cine = lgmax;
    for(i = n; i>=1; i--){
        if(poz[i] == cine){
            s[cine] = a[i];
            cine--;
        }
    }
    for(i = 1; i<=lgmax; i++){
        fout << s[i] << ' ';
    }
    fout << '\n';
}

int main()
{
    citire();
    construiescbest();
    afisare();

    return 0;
}