Cod sursa(job #2578527)

Utilizator vlad082002Ciocoiu Vlad vlad082002 Data 11 martie 2020 11:02:41
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.77 kb
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

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

int v[100005], n, pre[100005], best[100005], res = 1;

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

void solve() {
    best[1] = 1;
    for(int i = 2; i <= n; i++) {
        best[i] = 1;
        for(int j = 1; j < i; j++)
            if(v[j] < v[i] && best[i] < best[j]+1) {
                pre[i] = j;
                best[i] = best[j]+1;
            }
        if(best[res] < best[i]) res = i;
    }

    fout << best[res] << '\n';
}

void afis(int a) {
    if(!a) return;
    afis(pre[a]);
    fout << v[a] << ' ';
}

int main() {
    citire();
    solve();
    afis(res);
}