Cod sursa(job #2977796)

Utilizator Elvis_CostinTuca Elvis-Costin Elvis_Costin Data 12 februarie 2023 14:21:15
Problema Subsir crescator maximal Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.14 kb
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string np = "scmax";
ifstream f(np + ".in");
ofstream g(np + ".out");

// #define f cin
// #define g cout

ll n, len;
vector<ll> topuri;
// vector<stack<ll>> lista(100003);

ll cb(ll nr)
{
    ll st = 1, dr = len, mid, rez = -1;
    mid = (st + dr) / 2;
    while (st <= dr)
    {
        mid = (st + dr) / 2;
        if (nr <= topuri[mid])
            rez = mid, dr = mid - 1;
        else
            st = mid + 1;
    }
    return rez;
}
int main()
{
    f >> n;
    vector<ll> v(n + 1);
    for (ll i = 1; i <= n; i++)
        f >> v[i];

    map<ll, ll> ant;
    topuri.resize(n + 1);

    for (ll i = 1, poz; i <= n; i++)
    {
        poz = cb(v[i]);
        if (poz == -1)
            poz = len + 1, len = poz;
        // lista[poz].push(v[i]);
        topuri[poz] = v[i];
        ant[v[i]] = topuri[poz - 1];
    }

    vector<ll> rez;
    for (ll u = topuri[len]; u; u = ant[u])
        rez.push_back(u);
    reverse(rez.begin(), rez.end());

    g << rez.size() << '\n';
    for (auto x : rez)
        g << x << " ";

    return 0;
}