Cod sursa(job #1438752)

Utilizator greenadexIulia Harasim greenadex Data 20 mai 2015 19:23:08
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int MAX = 100001;
int dp[MAX], father[MAX], n, v[MAX], maxx, maxxi;
vector <int> s;

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

    for (int i = 1; i <= n; i++)
        for (int j = i - 1; j >= 1; j--)
            if(v[j] < v[i] && dp[j] + 1 > dp[i]) {
                dp[i] = dp[j] + 1;
                father[i] = j;
                if (dp[i] > maxx) {
                    maxx = dp[i];
                    maxxi = i;
                }
            }

    fout << maxx << '\n';

    s.push_back(maxxi);

    while (father[maxxi]) {
        s.push_back(father[maxxi]);
        maxxi = father[maxxi];
    }

    while (s.size()) {
        fout << v[s.back()] << ' ';
        s.pop_back();
    }

    return 0;
}