Cod sursa(job #2133811)

Utilizator IulianOleniucIulian Oleniuc IulianOleniuc Data 17 februarie 2018 12:27:56
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <fstream>
#define VMAX 100010

std::ifstream fin("scmax.in");
std::ofstream fout("scmax.out");

int n;
int v[VMAX];

int lgMax;
int sol[VMAX];

int dp[VMAX];
int posPrec[VMAX];

int main() {
    int max, posMax;

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

    dp[1] = 1;
    for (int i = 2; i <= n; i++) {
        max = 0;
        for (int j = 1; j < i; j++)
            if (dp[j] > max && v[j] < v[i])
                max = dp[posMax = j];

        dp[i] = max + 1;
        posPrec[i] = posMax;
    }

    lgMax = 0;
    for (int i = 1; i <= n; i++)
        if (dp[i] > lgMax) {
            lgMax = dp[i];
            posMax = i;
        }

    for (int i = lgMax; i >= 1; i--) {
        sol[i] = v[posMax];
        posMax = posPrec[posMax];
    }

    fout << lgMax << '\n';
    for (int i = 1; i <= lgMax; i++)
        fout << sol[i] << ' ';

    fout << '\n';
    fout.close();
    return 0;
}