Cod sursa(job #3280492)

Utilizator stefan_dore_Stefan Dore stefan_dore_ Data 26 februarie 2025 16:34:45
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;

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

int N;
vector<int> V, D, P, L;

void citire() {
    int x;
    f >> N;
    for (int i=1; i<=N; i++) {
        f >> x;
        V.push_back(x);
    }
}

void SCLM() {
    D.push_back(V[0]);
    P.push_back(0);
    for (int i=1; i<N; i++) {
        if (D.back() >= V[i]) {
            int poz = lower_bound(D.begin(), D.end(), V[i]) - D.begin();
            D[poz] = V[i];
            P.push_back(poz);
        } else {
            D.push_back(V[i]);
            P.push_back(D.size()-1);
        }
    }
}

void reconstituire() {
    int j = N-1;
    for (int i=D.size()-1; i >= 0; i--) {
        while(P[j] != i)
            j--;
        L.push_back(j);
    }
}

void afisare() {
    g << D.size() << '\n';
    for (int i=L.size()-1; i>=0; i--)
        g << V[L[i]] << ' ';
}

int main()
{
    citire();
    SCLM();
    reconstituire();
    afisare();
    f.close();
    g.close();
    return 0;
}