Cod sursa(job #2672604)

Utilizator CotoiRaresCotoi Rares CotoiRares Data 14 noiembrie 2020 11:42:10
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <bits/stdc++.h>
using namespace std;

ifstream in("scmax.in");
ofstream out("scmax.out");
vector <int> v[100005];

int n, x, lmax;

int cautarebinara(int poz1, int poz2, int nr)
{
    while (poz2 > poz1)
    {
        int mijloc = (poz2 + poz1) / 2;
        if (v[mijloc][mijloc] >= nr)
            poz2 = mijloc;
        else
            poz1 = mijloc + 1;
    }
    return poz2;
}

int main()
{
    in >> n;
    in >> x;
    v[0].push_back(x);
    lmax = 1;
    for (int i = 2; i <= n; ++i)
    {
        in >> x;
        int aux = cautarebinara(0, lmax, x);
        if (aux == 0)
        {
            v[0][0] = x;
            continue;
        }
        if (aux == lmax)
            lmax++;
        v[aux].clear();
        for (int j = 0; j < aux; ++j)
            v[aux].push_back(v[aux - 1][j]);

        v[aux].push_back(x);
    }
    out << lmax << '\n';
    for (int i = 0; i < lmax; ++i)
        out << v[lmax - 1][i] <<" ";
}