Cod sursa(job #1841013)

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

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

vector<int> v, l, poz;

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

int main()
{
    int n;
    f >> n;
    l.resize(n);
    poz.resize(n);
    for (int i = 0; i < n; ++i)
    {
        int x;
        f >> x;
        v.push_back(x);
    }
    scmax();
    int lg = 0, poz_max = -1;
    for (int i = 0; i < n; ++i)
        if (l[i] > lg)
        {
            poz_max = i;
            lg = l[i];
        }
    g << lg << "\n";
    while(poz_max != -1)
    {
        g << v[poz_max] << " ";
        poz_max = poz[poz_max];
    }
    return 0;
}