Cod sursa(job #3169462)

Utilizator zetef3Dediu Stefan zetef3 Data 15 noiembrie 2023 00:25:54
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.95 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream f("scmax.in");
ofstream g("scmax.out");

const int NMAX=1e5+1;
const int INF=0x3f3f3f;

int n,lmax;
int v[NMAX], d[NMAX], tata[NMAX], S[NMAX];

void citire() {
    f >> n;
    for (int i=1;i<=n;i++) {
        f >> v[i];
        d[i]=INF;
    }
}

void solve() {
    for (int i=1;i<=n;i++) {
        int st=1,dr=lmax,m=0,poz=0;

        while (st<=dr) {
            m=(st+dr)/2;

            if (v[i] > v[d[m]]) {
                st=m+1;
                poz=m;
            } else {
                dr=m-1;
            }
        }

        if (poz==lmax) {
            lmax++;
            d[lmax]=i;
            tata[i]=d[poz];
        } else {
            d[poz+1]=i;
            tata[i]=d[poz];
        }
    }
}

void afis(int i) {
    if (i>0) {
        afis(tata[i]);
        g << v[i] << ' ';
    }
}

int main()
{
    citire();
    solve();
    g << lmax << '\n';
    afis(d[lmax]);
    return 0;
}