Cod sursa(job #2333678)

Utilizator radugheoRadu Mihai Gheorghe radugheo Data 1 februarie 2019 19:31:07
Problema Subsir crescator maximal Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <bits/stdc++.h>

using namespace std;

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

int n, i, maxim, j, poz, k;
int v[100005], t[100005], d[100005], sol[100005];

int main(){
    fin >> n;
    for (i=1; i<=n; i++){
        fin >> v[i];
    }
    v[++n] = INT_MAX;
    for (i=2; i<=n; i++){
        maxim = 0;
        for (j=1; j<i; j++){
            if (v[j] < v[i] && d[j] > maxim){
                maxim = d[j];
                poz = j;
            }
        }
        if (maxim != 0){
            d[i] = maxim + 1;
            t[i] = poz;
        }
        else{
            d[i] = 1;
            t[i] = 0;
        }
    }
    while (n){
        sol[++k] = v[n];
        n = t[n];
    }
    fout << k - 1 << " \n";
    for (i=k; i>1; i--){
        fout << sol[i] << " ";
    }
    return 0;
}
// solutie in O(n^2)
/** recapitulare **/