Cod sursa(job #1349524)

Utilizator cosmin.pascaruPascaru Cosmin cosmin.pascaru Data 20 februarie 2015 11:55:05
Problema Subsir crescator maximal Scor 65
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 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];

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()
{
    fin >> n;
    for (int i = 1; i <= n; ++i)
    {
        fin >> a[i];
        best[ bsrc(a[i]) ] = a[i];
    }
    int poz = lg;
    for (int i = n; i >= 1; --i)
        if (a[i] >= best[poz] && a[i] < cmax)
        {
            sol[poz--] = a[i];
            cmax = a[i];
        }
    fout << lg << '\n';
    for (int i = 1; i <= lg; ++i)
        fout << sol[i] << ' ';
    fout << '\n';

    return 0;
}