Cod sursa(job #3298065)

Utilizator Dragu_AndiDragu Andrei Dragu_Andi Data 26 mai 2025 16:42:23
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

const int nmax = 1e5 + 1;
int v[nmax], dp[nmax], tati[nmax], last[nmax];

int main()
{
    int n;
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> v[i];
    int len = 1;
    dp[1] = v[1];
    last[1] = 1;
    tati[1] = 0;
    for (int i = 2; i <= n; i++)
    {
        int p = lower_bound(dp + 1, dp + len + 1, v[i]) - dp;

        dp[p] = v[i];
        last[p] = i;
        tati[i] = (p > 1) ? last[p - 1] : 0;

        if (p > len)
            len = p;
    }
    fout << len << '\n';
    int sol[nmax], idx = 0;
    int cur = last[len];
    while (cur)
    {
        sol[idx++] = v[cur];
        cur = tati[cur];
    }
    for (int i = idx - 1; i >= 0; i--)
        fout << sol[i] << ' ';
    return 0;
}