Cod sursa(job #3256241)

Utilizator _adeee18Adelina Maria _adeee18 Data 13 noiembrie 2024 21:25:21
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <fstream>

using namespace std;

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

int n, a[100001], l[100001], p ;

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

}

int det_l_max()
{
    int lmax = 0;
    for(int i = n; i >= 1; i--)
    {
        if(i == n)
            l[i] = 1;
        else
            for(int j = i + 1; j <= n; j++)
                if(a[i] < a[j])
                    if(l[j] + 1 > l[i])
                        l[i] = l[j] + 1;
        if(l[i] > lmax)
        {
            lmax = l[i];
            p = i;
        }

    }
    return lmax;
}

void det_elem_l_max()
{
    int j = p + 1;
    fout << a[p] << ' ';
    while(j <= n)
    {
        if(a[p] < a[j] && l[p] == l[j] + 1)
        {
            fout << a[j] << ' ';
            p = j;
        }
        j++;

    }
}

int main()
{
    citire();
    fout << det_l_max() << "\n";
    det_elem_l_max();
    return 0;
}