Cod sursa(job #1740244)

Utilizator AplayLazar Laurentiu Aplay Data 11 august 2016 11:38:51
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <bits/stdc++.h>

#define NMAX 100001

using namespace std;

int N, values[NMAX], X[NMAX], prev[NMAX];

int binarySearch(int length, int value) {
    int it, pow;

    for (pow = 1; pow < N; pow <<= 1);
    for (it = 0; pow; pow >>= 1) {
        if (it + pow <= length && values[X[it + pow]] < value)
            it += pow;
    }

    return it + 1;
}

int scMax() {
    int k, best = 1;

    X[1] = 0;
    prev[0] = -1;

    for (int it = 1; it < N; ++it) {
        k = binarySearch(best, values[it]);
        prev[it] = X[k - 1];
        X[k] = it;
        if (k > best) best = k;
    }

    return best;
}

void reconstruct(const int length) {
    int S[length], k = X[length];

    for (int it = length - 1; it >= 0; --it) {
        S[it] = values[k];
        k = prev[k];
    }

    for (int it = 0; it < length; ++it)
        printf("%d ", S[it]);
}

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

    scanf("%d", &N);
    for (int it = 0; it < N; ++it) {
        scanf("%d", values + it);
    }

    int best = scMax();
    printf("%d\n", best);
    reconstruct(best);

    return 0;
}