Cod sursa(job #1861210)

Utilizator preda.andreiPreda Andrei preda.andrei Data 28 ianuarie 2017 18:23:21
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <fstream>
#include <vector>

using namespace std;

int CautaLungime(const vector<int> &p, const vector<int> &v, int x)
{
    int pas = (1 << 20);
    int poz = -1;

    while (pas > 0) {
        if (poz + pas < p.size() && v[p[poz + pas]] < x) {
            poz += pas;
        }
        pas >>= 1;
    }
    return poz;
}

void RefaDrum(const vector<int> &v, const vector<int> &pred, int poz, ofstream &f)
{
    if (pred[poz] != -1) {
        RefaDrum(v, pred, pred[poz], f);
    }
    f << v[poz] << " ";
}

int main()
{
    ifstream fin("scmax.in");
    ofstream fout("scmax.out");

    int n;
    fin >> n;

    vector<int> elemente(n);
    vector<int> predecesor(n, -1);
    vector<int> poz_min;

    for (int i = 0; i < n; ++i) {
        fin >> elemente[i];

        int len = CautaLungime(poz_min, elemente, elemente[i]);
        if (len + 1 == poz_min.size()) {
            poz_min.push_back(i);
        } else {
            poz_min[len + 1] = i;
        }

        if (len != -1) {
            predecesor[i] = poz_min[len];
        }
    }

    fout << poz_min.size() << "\n";
    RefaDrum(elemente, predecesor, poz_min.back(), fout);

    return 0;
}