Cod sursa(job #2963599)

Utilizator Mihai-VijulieVijulie Mihai Mihai-Vijulie Data 11 ianuarie 2023 16:22:19
Problema Subsir crescator maximal Scor 30
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <fstream>

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

const int N = 1024;

int lung[N+1], x[N+1], n;

void refac_subsir(int p, int l, int val){
    if(l == 0)
        return;

    if(lung[p] == l && x[p] < val){
        refac_subsir(p-1,l-1,x[p]);
        fout<<x[p]<<' ';
    }else{
        refac_subsir(p-1,l,val);
    }
}

int main()
{
    fin>>n;
    int pmax = 1;
    for(int i = 1; i <= n; i++){
        int l_j = 0;
        fin>>x[i];
        for(int j = 1; j < i; j++){
            if(x[j] < x[i]){
                if(lung[j] > l_j)
                    l_j = lung[j];
            }
        }
        lung[i] = 1 + l_j;
        if(lung[i] > lung[pmax]) pmax = i;
    }
    fout<<lung[pmax]<<'\n';
    refac_subsir(pmax,lung[pmax], x[pmax]+1);
    return 0;
}