Cod sursa(job #2963593)

Utilizator TODEToderita Mihai TODE Data 11 ianuarie 2023 16:13:56
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <fstream>
using namespace std;

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

const int N = 1e6;
int l[N + 1] , x[N + 1] , n;
void refac_subsir(int dmax , int lmax , int val){
    if(lmax == 0){
        return;
    }
    if(x[dmax] < val && l[dmax] == lmax){
        refac_subsir(dmax - 1 , lmax - 1 , x[dmax]);
        out << x[dmax] << ' ';
    }
    else{
        refac_subsir(dmax - 1 , lmax , val);
    }
}
int main(){
    in >> n;
    for(int i = 1 ; i <= n ; i++){
        in >> x[i];
    }
    int poz_max = 1;
    for(int i = 1 ; i <= n ; i++){
        int l_max_i = 0;
        for(int j = 1 ; j < i ; j++){
            if(x[j] < x[i]){
                if(l[j] > l_max_i){
                    l_max_i = l[j];
                }
            }
        }
        l[i] = 1 + l_max_i;
        if(l[i] > l[poz_max]){
            poz_max = i;
        }
    }
    out << l[poz_max] << '\n';
    refac_subsir(poz_max , l[poz_max] , x[poz_max] + 1);
}