Cod sursa(job #1767396)

Utilizator preda.andreiPreda Andrei preda.andrei Data 28 septembrie 2016 22:57:26
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <fstream>
#include <vector>

using namespace std;

const int kLmax = 100001;

int GasestePozitie(int x, const vector<int> &num, const vector<int> &poz, int start)
{
    while (start > 0) {
        if (num[poz[start]] < x)
            return poz[start];
        --start;
    }
    return 0;
}

void Afiseaza(int poz, const vector<int> &pred, const vector<int> &val, ofstream &f)
{
    if (pred[poz] > 0)
        Afiseaza(pred[poz], pred, val, f);
    f << val[poz] << " ";
}

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

    int n;
    fin >> n;

    vector<int> numere(n + 1);
    vector<int> lungime_maxima(n + 1, 0);
    vector<int> predecesori(n + 1, 0);
    vector<int> pozitie_lungime(kLmax, 0);
    int lmax = 0, sfarsit_sir = 0;

    numere[0] = (1 << 31) - 1;
    for (int i = 1; i <= n; ++i) {
        fin >> numere[i];
        int x = numere[i];

        int poz = GasestePozitie(x, numere, pozitie_lungime, lmax);
        lungime_maxima[i] = lungime_maxima[poz] + 1;
        predecesori[i] = poz;
        if (x < numere[pozitie_lungime[lungime_maxima[i]]])
            pozitie_lungime[lungime_maxima[i]] = i;

        if (lungime_maxima[i] > lmax) {
            lmax = lungime_maxima[i];
            sfarsit_sir = i;
        }
    }

    fout << lmax << "\n";
    Afiseaza(sfarsit_sir, predecesori, numere, fout);
    return 0;
}