Cod sursa(job #3240863)

Utilizator XxThornadoxXStoica Teodora XxThornadoxX Data 22 august 2024 09:44:30
Problema Subsir crescator maximal Scor 15
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <iostream>
#include <fstream>

using namespace std;

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

long long n, a[1001], cnt = 0, rez[1001], sol[1001];

int main()
{
    int lmax = 1;

    fin >> n;

    for(int i = 0; i < n; i++)
        fin >> a[i];

    for(int i = 0; i < n; i++)
    {
        rez[i] = 1;
        for(int j = i - 1; j >= 0; j--)
        {
            if(a[i] > a[j] && rez[j] >= rez[i])
            {
                rez[i] = rez[j] + 1;
                if(rez[i] > lmax) lmax = rez[i];
            }
        }
    }
    fout << lmax << "\n";

    for(int i = n; i >= 0; i--)
    {
        if(rez[i] == lmax)
        {
            sol[cnt++] = i+1;
            lmax--;
        }
    }

    for(int i = cnt - 1; i >= 0; i--)
        fout << a[sol[i]] << " ";

    return 0;
}