Cod sursa(job #2913730)

Utilizator TarceaIonutTarcea Tudor Ionut TarceaIonut Data 16 iulie 2022 15:37:40
Problema Subsir crescator maximal Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>

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

int n, lmax, imax;
vector<int> a, d, b;

void read();
void make_bd();
int search_l(int val);
void afis();

int main()
{
    read();
    make_bd();
    afis();
    return 0;
}
void afis(){
    fout << lmax << '\n';
    stack<int> s;
    s.push(a[imax]);
    int k = lmax - 1;
    for (int i = imax - 1; i >= 0 && k > 0; i--){
        if (b[i] == k){
            s.push(a[i]);
            k--;
        }
    }
    while (!s.empty()){
        fout << s.top() << ' ';
        s.pop();
    }
}
int search_l(int val){
    int st = 0, dr = lmax, m, rez;
    while (st <= dr){
        m = (st + dr) / 2;
        if (val < d[m])
            dr = m - 1;
        else{
            st = m + 1; rez = m;
        }
    }
    return rez;
}
void make_bd(){
    d[1] = a[0]; lmax = 1; b[0] = 1;
    for (int i = 1; i < n; i++){
        if (a[i] == a[i-1])
            continue;
        int l = search_l(a[i]) + 1;
        if (l > lmax){  
            lmax = l; imax = i;
        }
        d[l] = a[i]; b[i] = l;
    }
}
void read()
{
    fin >> n;
    a = d = b = vector<int>(n+1);
    for (int i = 0; i < n; i++)
        fin >> a[i];
}