Cod sursa(job #2451975)

Utilizator IoanaDraganescuIoana Draganescu IoanaDraganescu Data 29 august 2019 00:32:43
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.98 kb
#include <iostream>
#include <fstream>
#include <stack>

using namespace std;

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

const int inf = 0x3f3f3f3f;

int v[100005], d[100005], prec[100005], n, lmax;
stack <int> st;

int cautbin (int val){
    int st = 0, dr = lmax + 1;
    while (dr - st > 1){
        int mij = (st + dr) >> 1;
        if (v[d[mij]] >= val)
            dr = mij;
        else
            st = mij;
    }
    return dr;
}

int main(){
    fin >> n;
    for (int i = 1; i <= n; i++)
        fin >> v[i];
    v[0] = inf;
    for (int i = 1; i <= n; i++){
        int l = cautbin(v[i]);
        prec[i] = d[l - 1];
        d[l] = i;
        if (l > lmax)
            lmax = l;
    }
    int poz = d[lmax];
    fout << lmax << '\n';
    while (poz){
        st.push(v[poz]);
        poz = prec[poz];
    }
    while (!st.empty()){
        fout << st.top() << ' ';
        st.pop();
    }
    fout << '\n';
    return 0;
}