Cod sursa(job #2848925)

Utilizator PalffyLehelPalffy Lehel PalffyLehel Data 14 februarie 2022 10:56:54
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream f("scmax.in");
    ofstream g("scmax.out");

    int n;
    f >> n;

    int szamok[n];

    for (int i = 0; i < n; i++)
    {
        f >> szamok[i];
    }

    int hossz[n];
    int elottem[n];
    fill_n(elottem, n, -1);
    fill_n(hossz, n, 0);
    int maxi, maxiIndex;

    for (int i = n - 1; i >= 0; i--)
    {
        maxi = 0;
        maxiIndex = -1;
        for (int j = n - 1; j > i; j--)
        {
            if (szamok[i] < szamok[j] && maxi < hossz[j])
            {
                maxi = hossz[j];
                maxiIndex = j;
            }
        }

        hossz[i] = maxi + 1;
        elottem[i] = maxiIndex;
    }

    maxi = -1;
    for (int i = 0; i < n; i++)
    {
        if (hossz[i] > maxi)
        {
            maxi = hossz[i];
            maxiIndex = i;
        }
    }

    g << hossz[maxiIndex] << endl;
    for (; maxiIndex != -1; maxiIndex = elottem[maxiIndex])
    {
        g << szamok[maxiIndex] << " ";
    }



    return 0;
}