Cod sursa(job #1522350)

Utilizator zacuscaAlex Iordache zacusca Data 11 noiembrie 2015 16:43:27
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <fstream>

using namespace std;

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

int n, sol, V[100001], End[100001], Pred[100001];

void afis(int x)
{
    if(x > 0)
        {
            afis(Pred[x]);
            out << V[x] << ' ';
        }
}

int main()
{
    in >> n;

    for (int i = 1; i <= n; i++) in >> V[i];

    for (int i = 1; i <= n; i++)
    {
        int step = (1 << 16), poz = 0;
        for (; step; step >>= 1)
            if (poz + step <= sol and V[End[poz + step]] < V[i])
            poz += step;

        poz ++;

        End[poz] = i;

        if(poz == 1) Pred[i] = 0;
        else Pred[i] = End[poz - 1];

        sol = max(sol, poz);
    }

    out << sol << '\n';

    afis(End[sol]);

    out.close();
    return 0;
}