Cod sursa(job #2291970)

Utilizator flee123Flee Bringa flee123 Data 28 noiembrie 2018 20:37:22
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <fstream>

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

int sequence[100005], parent[100005], v[100005], solutii[100005],n, lmax, last_position;

int caut_bin(int elem)
{
    int st = 0, dr = lmax, m;
    while (st < dr)
    m = (st + dr) >> 1, (v[sequence[m]] < elem) ? st = m + 1 : dr = m;
    return st;
}

int main()
{
    fin >> n;
    int i = 0, pos;
    while(fin >> v[i])
        sequence[i] = 0, parent[i++] = -1;
    for (i = 1; i < n; i++)
    (v[i] < v[sequence[0]]) ? sequence[0] = i : v[i] > v[sequence[lmax]] ? parent[i] = sequence[lmax],lmax++,sequence[lmax] = i,last_position = i : (pos = caut_bin(v[i]), parent[i] = sequence[pos - 1],sequence[pos] = i);
    int copie = lmax;
    fout << lmax + 1 << '\n';
    while (parent[last_position] != -1)
            solutii[copie--] = v[last_position], last_position = parent[last_position];
        solutii[copie] = v[last_position];
        for (i = 0; i <= lmax; i++)
            fout << solutii[i] << ' ';

    return 0;
}