Cod sursa(job #2315496)

Utilizator papinub2Papa Valentin papinub2 Data 10 ianuarie 2019 00:18:12
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

int main()
{
    int n, poz, maxim = 0;

    in.sync_with_stdio(false);
    in >> n;

    vector<int> v(n + 1);
    vector<int> best(n + 1);
    vector<int> next(n + 1);

    for (int i = 1; i <= n; i++)
        in >> v[i];

    for (int i = n - 1; i >= 1; i--)
        for (int j = i + 1; j <= n; j++)
            if (v[i] < v[j])
                if (best[i] < best[j] + 1)
                    best[i] = best[j] + 1, next[i] = j;

    for (int i = 1; i <= n; i++)
        if (best[i] > maxim)
            maxim = best[i], poz = i;

    out << maxim + 1 << '\n';
    while (poz)
    {
        out << v[poz] << ' ';
        poz = next[poz];
    }

    return 0;
}