Cod sursa(job #1841399)

Utilizator crazylamaRiclea Andrei crazylama Data 5 ianuarie 2017 16:49:07
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 2.52 kb
#include <fstream>
#include <vector>
#include <cmath>
#include <iostream>
using namespace std;

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

vector<int> v, poz;
int n;

struct Element {
    int lungime_max, poz_elem, val;
};

vector<Element> arbInt;


/*void scmax()
{
    int n = v.size() - 1;
    l[n] = 1;
    poz[n] = -1;
    for (int i = n - 1; i > 0; --i)
    {
        l[i] = 1;
        poz[i] = -1;
        for (int j = i + 1; j <= n; ++j)
            if (v[i] < v[j] && l[j] + 1 >= l[i])
            {
                l[i] = l[j] + 1;
                poz[i] = j;
            }
    }
}*/

Element query(int nod, int st, int dr, int val_prec)
{
    Element q;
    q.lungime_max = -1;
    q.poz_elem = -1;
    q.val = -1;
    if (st <= dr)
    {
        if (arbInt[nod].val > val_prec)
            return arbInt[nod];
        int mij = st + (dr - st) / 2;
        if (st < dr)
        {
            Element fHalf = query(nod << 1, st, mij, val_prec);
            Element sHalf = query((nod << 1) + 1, mij + 1, dr, val_prec);
            if (fHalf.lungime_max != -1 && fHalf.lungime_max > sHalf.poz_elem)
                return fHalf;
            return sHalf;
        }
    }
    return q;
}

void update(int nod, int st, int dr, int pos, int valoare)
{
    if (st == dr)
    {
        Element L_max = query(1, pos + 1, n, v[st]);
        if (L_max.lungime_max != -1)
            arbInt[nod].lungime_max = L_max.lungime_max + 1;
        else
            arbInt[nod].lungime_max = 1;
        arbInt[nod].poz_elem = st;
        arbInt[nod].val = v[st];
        poz[pos] = L_max.poz_elem;
        return;
    }
    int mij = st + (dr - st) / 2;
    if (pos <= mij)
        update(nod << 1, st, mij, pos, valoare);
    else
        update((nod << 1) + 1, mij + 1, dr, pos, valoare);
    if (arbInt[nod << 1].lungime_max > arbInt[(nod << 1) + 1].lungime_max)
        arbInt[nod] = arbInt[nod << 1];
    else
        arbInt[nod] = arbInt[(nod << 1) + 1];
}

void scmax()
{
    poz[n] = -1;
    update(1, 1, n, n, 1);
    for (int i = n - 1; i > 0; --i)
    {
        update(1, 1, n, i, 1);
    }
}

int main()
{
    f >> n;
    poz.resize(n + 1);
    v.resize(n + 1);
    arbInt.resize(4 * n + 1);
    for (int i = 1; i <= n; ++i)
        f >> v[i];
    scmax();
    int lg = arbInt[1].lungime_max, poz_max = arbInt[1].poz_elem;
    g << lg << "\n";
    while(poz_max != -1)
    {
        g << v[poz_max] << " ";
        poz_max = poz[poz_max];
    }
    return 0;
}