Cod sursa(job #2674814)

Utilizator iancupoppPopp Iancu Alexandru iancupopp Data 20 noiembrie 2020 13:28:55
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>
#include <vector>

using namespace std;

const int N = 1e5;

int v[N], l[N];
vector<int> vmin;

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

int cbin(int x) {
    if (vmin.empty() || x > vmin.back())
        return vmin.size();
    int st = 0, dr = vmin.size() - 1, m;
    while (st < dr) {
        m = (st + dr) / 2;
        if (vmin[m] >= x)
            dr = m;
        else
            st = m + 1;
    }
    return st;
}

void afis(int lc, int poz) {
    if (!lc)
        return;
    while (l[poz] != lc)
        --poz;
    afis(lc - 1, poz - 1);
    out << v[poz] << ' ';
}

int main() {
    int n, poz, lmax = 0;
    in >> n;
    for (int i = 0; i < n; ++i) {
        in >> v[i];
        poz = cbin(v[i]);
        if (poz == vmin.size())
            vmin.push_back(v[i]);
        else
            vmin[poz] = v[i];
        l[i] = poz + 1;
        lmax = max(lmax, l[i]);
    }
    out << lmax << '\n';
    afis(lmax, n - 1);

    in.close();
    out.close();
    return 0;
}