Cod sursa(job #3285304)

Utilizator stefanrotaruRotaru Stefan-Florin stefanrotaru Data 12 martie 2025 17:59:14
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

int n, a[100005], poz;

vector <int> ans, aux, pozitii, precedent;

int main()
{
    f >> n;

    for (int i = 1; i <= n; ++i) {
        f >> a[i];
    }

    aux.push_back(a[1]);
    pozitii.push_back(1);
    precedent.resize(n + 1, -1);

    for (int i = 2; i <= n; ++i) {
        if (a[i] > aux.back()) {
            aux.push_back(a[i]);
            pozitii.push_back(i);
            precedent[i] = pozitii[(int) pozitii.size() - 2];
        }
        else {
            poz = lower_bound(aux.begin(), aux.end(), a[i]) - aux.begin();

            aux[poz] = a[i];
            pozitii[poz] = i;

            if (poz > 0) {
                precedent[i] = pozitii[poz - 1];
            }
        }
    }

    int ultimul = pozitii.back();

    while (ultimul != -1) {
        ans.push_back(a[ultimul]);
        ultimul = precedent[ultimul];
    }

    g << (int) ans.size() << '\n';

    reverse(ans.begin(), ans.end());

    for (auto it : ans) {
        g << it << ' ';
    }

    return 0;
}