Cod sursa(job #1519116)

Utilizator tudorv96Tudor Varan tudorv96 Data 6 noiembrie 2015 20:57:18
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.87 kb
#include <fstream>
using namespace std;

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

const int N = 1e5 + 5;

int v[N], last[N], t[N], best[N], n, sol;

int bs(int x) {
    int poz = 0, step = 1;
    while ((step << 1) <= n)
        step <<= 1;
    for (; step; step >>= 1)
        if (poz + step <= sol && 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] = sol = 1;
    for (int i = 2; i <= n; ++i) {
        int poz = bs(v[i]);
        t[i] = last[poz];
        best[i] = best[last[poz]] + 1;
        last[poz + 1] = i;
        if (poz + 1 > sol)
            sol = poz + 1;
    }
    fout << sol << "\n";
    write(last[sol]);
}