Cod sursa(job #2964262)

Utilizator AndreiN96Andrei Nicula AndreiN96 Data 12 ianuarie 2023 18:51:58
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.01 kb
#include <fstream>

using namespace std;

const int N = 100000;
int v[N + 1];
int lung[N + 1];

void afis_subsir(int p, int lmax, ofstream &out)
{
    if (p == 0)
    {
        return;
    }
    if (lung[p] != lmax - 1)
    {
        afis_subsir(p - 1, lmax, out);
    }
    else
    {
        afis_subsir(p - 1, lung[p], out);
        out << v[p] << ' ';
    }
}
int main()
{
    ifstream in("scmax.in");
    ofstream out("scmax.out");

    int n;
    in >> n;
    int pmax = 1;
    for (int i = 1; i <= n; i ++)
    {
        in >> v[i];
        int lmax = 0;
        for (int j = 1; j < i; j ++)
        {
            if (v[j] < v[i])
            {
                lmax = max(lmax, lung[j]);
            }
        }
        lung[i] = lmax + 1;
        if (lung[i] > lung[pmax])
        {
            pmax = i;
        }
    }

    out << lung[pmax] << '\n';
    afis_subsir(pmax, lung[pmax], out);
    out << v[pmax] << ' ';

    in.close();
    out.close();

    return 0;
}