Cod sursa(job #2013979)

Utilizator sfechisalin@yahoo.comSfechis Alin [email protected] Data 22 august 2017 17:45:18
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.53 kb
#include <bits/stdc++.h>

#define LSB(x) ((x) & (-x))
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");

int N, h, best, start;
vector<int>a, BackUp, father, aux, Best, AIB;

void update(int poz, int ind)
{
    for (; poz <= N; poz += LSB(poz))
        if (Best[AIB[poz]] < Best[ind])
            AIB[poz] = ind;
}

int query(int nod)
{
    int answ = 0;
    for(; nod; nod -= LSB(nod))
        if ( Best[AIB[nod]] > Best[answ])
            answ = AIB[nod];
    return answ;
}

int writeSol(int nod)
{
    if (father[nod])
        writeSol(father[nod]);
    fout << BackUp[nod] << " ";
}

int main()
{
    fin >> N;
    a.resize(N + 1); BackUp.resize(N + 1, 0); father.resize(N + 1, 0); aux.resize(N + 1);
    Best.resize(N + 1, 0); AIB.resize(N + 1);

    for(int i = 1; i <= N; ++i)
    {
        fin >> a[i];
        BackUp[i] = aux[i] = a[i];
    }
    sort(aux.begin() + 1, aux.end());

    h = 1;
    for_each(aux.begin() + 2, aux.end(),[&h](int a) {
                if (aux[h] != a)
                    aux[++h] = a;
             });

    for (int i = 1; i <= N; ++i)
       a[i] = lower_bound(aux.begin() + 1, aux.begin() + h + 1, a[i]) - aux.begin();

    for (int i = 1; i <= N; ++i)
    {
        father[i] = query(a[i] - 1);
        Best[i] =  Best[father[i]] + 1;
        update(a[i], i);
        if (Best[i] > best)
        {
            best = Best[i];
            start = i;
        }
    }

    fout << best << "\n";
    writeSol(start);

    return 0;
}