Cod sursa(job #1349793)

Utilizator cosmin.pascaruPascaru Cosmin cosmin.pascaru Data 20 februarie 2015 14:52:41
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>

#define NMAX 100005
using namespace std;

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

int n, lg, cmax = 2000000009;
int a[NMAX], best[NMAX], sol[NMAX], prec[NMAX];

int bsrc(int x)
{
    int step = 1, rez = 0;
    while (step < lg) step <<= 1;

    for (; step; step >>= 1)
        if (best[rez + step] < x && rez + step <= lg) rez += step;
    if (rez == lg) ++lg;
    return rez + 1;
}
int main()
{
    int poz;
    fin >> n;
    for (int i = 1; i <= n; ++i)
    {
        fin >> a[i];
        poz = bsrc(a[i]);
        best[poz] = a[i];
        prec[i] = poz;
    }
    poz = lg+1;
    int j = n;
    for (int i = lg; i >= 1; --i)
    {
        while (prec[j] != i) --j;
        sol[--poz] = a[j];
    }

    fout << lg << '\n';
    for (int i = 1; i <= lg; ++i)
        fout << sol[i] << ' ';
    fout << '\n';

    return 0;
}