Cod sursa(job #1414122)

Utilizator andreea_zahariaAndreea Zaharia andreea_zaharia Data 2 aprilie 2015 13:10:23
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.94 kb
#include <cstdio>

#define MULT 2000000001
#define N 100000

int v[N +1], stiva[N +1], recon[N +1];
int n;

void afiseaza (int x) {
    if (x == 0) {
        return ;
    }

    afiseaza (recon[x]);
    printf ("%d ", v[x]);
}

int main () {
    freopen ("scmax.in", "r", stdin);
    freopen ("scmax.out", "w", stdout);

    scanf ("%d", &n);
    for (int i = 1; i <= n; i++) {
        scanf ("%d", &v[i]);
    }
    v[0] = MULT;

    int maxSecv = 0;
    for (int i = 1; i <= n; i++) {
        int poz = 0;
        for (int pas = 1 << 17; pas; pas >>=1) {
            if (poz + pas <= maxSecv && v[stiva[poz + pas]] < v[i]) {
                poz += pas;
            }
        }
        poz++;

        if (poz > maxSecv) {
            maxSecv = poz;
        }

        stiva[poz] = i;
        recon[i] = stiva[poz - 1];
    }

    printf ("%d\n", maxSecv);

    afiseaza (stiva[maxSecv]);

    return 0;
}