Cod sursa(job #3194294)

Utilizator rapidu36Victor Manz rapidu36 Data 17 ianuarie 2024 17:00:44
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <fstream>

using namespace std;

const int N = 1e5;

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

int v[N], l_max[N];

void refac_subsir(int poz, int val, int lungime)
{
    if (lungime == 0)
    {
        return;
    }
    if (v[poz] <= val && l_max[poz] == lungime)
    {
        refac_subsir(poz - 1, v[poz] - 1, lungime - 1);
        out << v[poz] << " ";
    }
    else
    {
        refac_subsir(poz - 1, val, lungime);
    }
}

int main()
{
    int n, poz_l_max = 0;
    in >> n;
    for (int i = 0; i < n; i++)
    {
        in >> v[i];
        int lungime_i = 0;
        for (int j = 0; j < i; j++)
        {
            if (v[j] < v[i])
            {
                lungime_i = max(lungime_i, l_max[j]);
            }
        }
        l_max[i] = lungime_i + 1;
        if (l_max[i] > l_max[poz_l_max])
        {
            poz_l_max = i;
        }
    }
    out << l_max[poz_l_max] << "\n";
    refac_subsir(poz_l_max, v[poz_l_max], l_max[poz_l_max]);
    in.close();
    out.close();
    return 0;
}