Cod sursa(job #3326107)

Utilizator DAnnipopPOp DAniel DAnnipop Data 27 noiembrie 2025 12:44:18
Problema Subsir crescator maximal Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <fstream>
using namespace std;

int main()
{
    ifstream fin("scmax.in");
    ofstream fout("scmax.out");

    int a[100001], lung[100001], poz[100001];
    int n, i, j;

    fin >> n;
    for (i = 1; i <= n; i++)
        fin >> a[i];

    lung[n] = 1;
    poz[n] = -1;

    for (i = n - 1; i >= 1; i--)
    {
        lung[i] = 1;
        poz[i] = -1;

        for (j = n; j > i; j--)
            if (a[i] < a[j] && lung[i] < lung[j] + 1)
            {
                lung[i] = lung[j] + 1;
                poz[i] = j;
            }
    }

    int maxim = -1, pozitie = -1;
    for (i = 1; i <= n; i++)
        if (lung[i] > maxim)
        {
            maxim = lung[i];
            pozitie = i;
        }

    fout << maxim << "\n";

    while (pozitie != -1)
    {
        fout << a[pozitie] << " ";
        pozitie = poz[pozitie];
    }

    return 0;
}