Cod sursa(job #2963582)

Utilizator 55andreiv55Andrei Voicu 55andreiv55 Data 11 ianuarie 2023 15:58:41
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>

using namespace std;
const int N = 1e5;

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

int dp[N + 1], x[N + 1];

void refs(int p, int l, int val)
{
    if (l == 0)
        return;
    if (dp[p] == l && x[p] < val)
    {
        refs(p - 1, l - 1, x[p]);
        out << x[p] << " ";
    }
    else
        refs(p - 1, l, val);
}

int main()
{
    int n, pmax = 1;
    in >> n;
    for (int i = 1; i <= n; i++)
    {
        int lj = 0;
        in >> x[i];
        for (int j = 1; j < i; j++)
        {
            if (x[j] < x[i])
            {
                if (dp[j] > lj)
                    lj = dp[j];
            }
        }
        dp[i] = 1 + lj;
        if (dp[i] > dp[pmax])
            pmax = i;
    }
    out << dp[n] << "\n";
    refs(pmax, dp[pmax], x[pmax] + 1);
    return 0;
}