Cod sursa(job #3030019)

Utilizator t_avramAvram Tudor t_avram Data 17 martie 2023 13:30:08
Problema Subsir crescator maximal Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <iostream>
#include <cstring>
using namespace std;

int n, v[100001], best[100001], poz[100001], Max, p, sol;;

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> v[i];
    }
    best[n] = 1;
    poz[n] = -1;
    Max = 1;
    p = n;
    for (int i = n - 1; i >= 1; i--)
    {
        best[i] = 1;
        poz[i] = -1;
        for (int j = i + 1; j <= n; j++)
        {
            if (v[i] < v[j] && best[i] < best[j] + 1)
            {
                best[i] = best[j] + 1;
                poz[i] = j;
                if (best[i] > Max)
                {
                    Max = best[i];
                    p = i;
                }
            }
        }
    }
    cout << Max << '\n';
    while (p != -1)
    {
        cout << v[p] << ' ';
        p = poz[p];
    }
    return 0;
}