Cod sursa(job #1896803)

Utilizator andreinmAndrei andreinm Data 28 februarie 2017 22:03:01
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.76 kb
#include <fstream>

using namespace std;

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

const int NM = 100010;

int N, i, j, lg, index;
int v[NM], D[NM], pre[NM];

int main()
{
    in >> N;
    for (i = 1; i <= N; ++i)
        in >> v[i];

    D[N] = 1; pre[N] = -1;

    for (i = N - 1; i >= 1; --i) {
        D[i] = 1; pre[i] = -1;

        for (j = i + 1; j <= N; ++j) {
            if (v[i] < v[j] && D[i] < D[j] + 1) {
                D[i] = D[j] + 1;
                pre[i] = j;
            }
        }
    }

    for (i = 1; i <= N; ++i) {
        if (D[i] > lg) {
            lg = D[i];
            index = i;
        }
    }

    out << lg << '\n';

    while (index != -1) {
        out << v[index] << ' ';
        index = pre[index];
    }

    return 0;
}