Cod sursa(job #2004377)

Utilizator LucaSeriSeritan Luca LucaSeri Data 25 iulie 2017 18:30:17
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include <bits/stdc++.h>

using namespace std;

const int MaxN = 100002;

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

struct elem{
    int val, unde = -1;
}dp[MaxN];
int main()
{
    int n;
    f >> n;
    int v[MaxN];
    for(int i = 0; i < n; ++i){
        f >> v[i];
    }
    int best = -1;
    int ind = -1;
    for(int i = 0; i < n; ++i){
        dp[i].val = 1;
        for(int j = 0; j < i; ++j){
            if(v[j] < v[i]){
                if(dp[j].val + 1 > dp[i].val){
                    dp[i].val = dp[j].val+1;
                    dp[i].unde = j;
                }
            }
        }
        if(dp[i].val > best){
            best = dp[i].val;
            ind = i;
        }
    }

    g << best << '\n';
    vector <int> aux;
    aux.push_back(v[ind]);
    while(dp[ind].unde != -1){
        aux.push_back(v[dp[ind].unde]);
        ind = dp[ind].unde;
    }

    for(int i = aux.size() -1; i >= 0; --i){
        g << aux[i] << ' ';
    }
    return 0;
}