Cod sursa(job #2848923)

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

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

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


 /*   for (int i = 0; i < n; i++)
    {
        cout << szamok[i] << " ";
    }

    cout << endl;

    for (int i = 0; i < n; i++)
    {
        cout << hossz[i] << " ";
    }

    cout << endl;

    for (int i = 0; i < n; i++)
    {
        cout << elottem[i] << " ";
    }

    cout << endl;*/

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


    stack<int> megoldas;

    for (; maxiIndex != -1; maxiIndex = elottem[maxiIndex])
    {
        megoldas.push(szamok[maxiIndex]);
    }

    g << megoldas.size() << endl;
    while(!megoldas.empty())
    {
        g << megoldas.top() << " ";
        megoldas.pop();
    }

    return 0;
}