Cod sursa(job #2581396)

Utilizator vlad082002Ciocoiu Vlad vlad082002 Data 15 martie 2020 10:20:37
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <fstream>
#include <iostream>
using namespace std;

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

int n, v[100005], pre[100005], lungime[100005], lgmax;

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

void solve() {
    for(int i = 1; i <= n; i++) {
        int st = 1, dr = lgmax, found = 0;
        if(v[i] > v[lungime[lgmax]]) found = lgmax+1;
        while(st <= dr) {
            int m = (st+dr)/2;
            if(v[lungime[m]] >= v[i] && v[lungime[m-1]] < v[i]) {
                found = m;
                break;
            } else if(v[lungime[m]] >= v[i])
                dr = m-1;
            else
                st = m+1;
        }
        lungime[found] = i;
        lgmax = max(lgmax, found);
        pre[i] = lungime[found-1];
    }
    fout << lgmax << '\n';
}

void afis(int poz) {
    if(!poz) return;
    afis(pre[poz]);
    fout << v[poz] << ' ';
}

int main() {
    citire();
    solve();
    afis(lungime[lgmax]);
}