Cod sursa(job #2268797)

Utilizator papinub2Papa Valentin papinub2 Data 25 octombrie 2018 12:28:52
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>
#include <vector>
#include <stack>

using namespace std;

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

int main()
{
    int n, start, maxim = 0;
    in >> n;

    stack<int> stk;
    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];
        best[i] = 1;

        for (int j = i - 1; j >= 1; j--)
            if (v[j] < v[i] && best[j] + 1 > best[i])
            {
                best[i] = best[j] + 1;
                next[i] = j;
                if (best[i] > maxim)
                {
                    maxim = best[i];
                    start = i;
                }
            }
    }

    out << maxim << '\n';
    while (start)
    {
        stk.push(start);
        start = next[start];
    }

    while (!stk.empty())
    {
        out << v[stk.top()] << ' ';
        stk.pop();
    }

    return 0;
}