Cod sursa(job #2658270)

Utilizator Rares31100Popa Rares Rares31100 Data 13 octombrie 2020 17:00:57
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <bits/stdc++.h>
#define PII pair<int,int>
#define poz second
#define val first

using namespace std;

ifstream in("scmax.in");
ofstream out("scmax.out");
int n, sir[100001];
PII lung[100001]; int lmax, urm[100001];
stack <int> sol;

int pozInLung(int val)
{
    int poz = 0;
    for(int p = lmax; p; p/=2)
        while(poz+p <= lmax && val > lung[poz+p].val)
            poz += p;

    return poz + 1;
}

int main()
{
    in >> n;

    for(int i = 1; i <= n; i++)
        in >> sir[i];

    lung[1] = {sir[1], 1};
    lmax = 1;

    for(int i = 2; i <= n; i++)
    {
        int poz = pozInLung(sir[i]);
        lung[poz] = {sir[i], i};
        urm[i] = lung[poz-1].poz;
        lmax = max(lmax, poz);
    }

    int poz = lung[lmax].poz;
    while(poz)
    {
        sol.push(sir[poz]);
        poz = urm[poz];
    }

    out << lmax << '\n';

    while(!sol.empty())
    {
        out << sol.top() << ' ';
        sol.pop();
    }

    return 0;
}