Cod sursa(job #2478072)

Utilizator BogdanRazvanBogdan Razvan BogdanRazvan Data 21 octombrie 2019 16:57:53
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.73 kb
#include <bits/stdc++.h>

using namespace std;

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

const int N = 100005;

int a[N], best[N], dad[N];

void path(int k)
{
    if(dad[k] != 0) path(dad[k]);
    fout << a[k] << " ";
}

int main()
{
    ios::sync_with_stdio(false);
    fin.tie(0);

    int n, m;
    int mx = 0, pos = -1;

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

    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j < i; ++j) {
            if(a[i] > a[j] && best[j] + 1 > best[i]) best[i] = best[j] + 1, dad[i] = j;
        }
        if(best[i] > mx) pos = i;
        mx = max(mx, best[i]);
    }
    fout << best[pos] + 1 << "\n";
    path(pos);
    return 0;
}