Cod sursa(job #1165345)

Utilizator tudorv96Tudor Varan tudorv96 Data 2 aprilie 2014 17:19:55
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <fstream>
using namespace std;

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

const int N = 1e5 + 5;

int best[N], v[N], t[N], last[N], nr = 1, n;

int bs(int x) {
    int poz = 0, step = 1;
    while (step <= nr)
        step <<= 1;
    for (; step; step >>= 1)
        if (poz + step <= nr && v[last[poz + step]] < x)
            poz += step;
    return poz;
}

void write (int x) {
    if (t[x])
        write (t[x]);
    fout << v[x] << " ";
}

int main() {
    fin >> n;
    for (int i = 1; i <= n; ++i)
        fin >> v[i];
    best[1] = last[1] = 1;
    for (int i = 2; i <= n; ++i) {
        int crt = bs(v[i]);
        t[i] = last[crt];
        best[i] = crt + 1;
        last[crt + 1] = i;
        if (nr < crt + 1)
            nr = crt + 1;
    }
    fout << nr << "\n";
    write (last[nr]);
}