Cod sursa(job #2636312)

Utilizator vlad082002Ciocoiu Vlad vlad082002 Data 17 iulie 2020 14:47:27
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.76 kb
#include <fstream>
using namespace std;

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

int dp[100005], v[100005], best, n,pre[100005];

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

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

void print(int x) {
    if(x) {
        print(pre[x]);
        fout << v[x] << ' ';
    }
}

void afis() {
    fout << dp[best] << '\n';
    print(best);
}

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