Cod sursa(job #1338650)

Utilizator Andrei_TirpescuAndrei Tirpescu Andrei_Tirpescu Data 10 februarie 2015 10:37:41
Problema Subsir crescator maximal Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 1 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 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 = pb;
            while(a[i] < best[aux])aux--;
            ++aux;
            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';
}