Cod sursa(job #2591298)

Utilizator k2e0e0w3qDumitrescu Gheorghe k2e0e0w3q Data 30 martie 2020 12:13:32
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>
using namespace std;

ofstream fout ("scmax.out");
int *v, *link;
void show (int poz) {
    if (poz!=-1) {
        show(link[poz]);
        fout << v[poz] << ' ';
    }
}
int main () {
    ifstream fin ("scmax.in");
    ios::sync_with_stdio(false);

    vector <int> deck, pos;
    pos.push_back(-1);
    int n, i;
    fin >> n;
    v=new int[n], link=new int[n];
    for (i=0; i<n; i++) {
        fin >> v[i];
        auto it=upper_bound(deck.begin(), deck.end(), v[i]-1);
        if (it==deck.end())
            deck.push_back(v[i]),
            link[i]=pos.back(),
            pos.push_back(i);
        else
            *it=v[i],
            pos[it-deck.begin()+1]=i,
            link[i]=pos[it-deck.begin()];
    }

    fout << deck.size() << '\n';
    show(pos.back());
    return 0;
}