Cod sursa(job #2883159)

Utilizator MateiAruxandeiMateiStefan MateiAruxandei Data 1 aprilie 2022 11:04:23
Problema Subsir crescator maximal Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.99 kb
#include <bits/stdc++.h>

using namespace std;

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

using ll = long long;
const int NMAX(100005);
int best[NMAX], lst[NMAX], ind, rez[NMAX], v[NMAX];

inline void insertX(int val, int pz){
    if(val > best[ind]){
        ++ind;
        best[ind] = val;
        lst[pz] = ind;
        return;
    }
    int step = 1;
    for(;step <= ind; step <<= 1);
    step >>= 1;

    int poz = 0;
    for(; step; step >>= 1)
        if(poz + step <= ind && best[poz + step] < val)
            poz += step;
    ++poz;
    best[poz] = val;
    lst[pz] = ind;
}

int main()
{
    int n;
    fin >> n;

    for(int i = 1; i <= n; ++i){
        fin >> v[i];
        insertX(v[i], i);
    }

    int pz = n;
    for(int i = ind; i >= 1; --i){
        while(lst[pz] != i)
            --pz;
        rez[i] = v[pz];
    }

    fout << ind << '\n';
    for(int i = 1; i <= ind; ++i)
        fout << rez[i] << ' ';
    return 0;
}