Cod sursa(job #1885863)

Utilizator vladbatalanBatalan Vlad vladbatalan Data 20 februarie 2017 14:38:58
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 0.85 kb
#include <bits/stdc++.h>

using namespace std;

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

int best[100010], p[100010];
int n, v[100010], n_max, p_max;
vector<int> sol;

int main()
{
    fin >> n;
    for(int i=1; i<=n; i++){
        fin >> v[i];
        best[i] = 1;
    }
    n_max = 1; p_max = 1;
    for(int i=2; i<=n; i++)
    {
        for(int j=1; j<i; j++)
            if(v[j] < v[i] && best[i] < best[j]+1){
                best[i] = best[j] + 1;
                p[i] = j;
            }
        if(best[i] > n_max)
        {
            n_max = best[i];
            p_max = i;
        }
    }
    while(p_max != 0)
    {
        sol.push_back(p_max);
        p_max = p[p_max];
    }
    sort(sol.begin(), sol.end());
    fout<<sol.size()<<'\n';
    for(auto it:sol)
        fout<<v[it]<<' ';
    return 0;
}