Cod sursa(job #1888597)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 22 februarie 2017 11:28:09
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <fstream>

using namespace std;

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

int n, i, j, a[100005], sol[100005];
int bst[100005], k, poz[100005];

int main() {
    f >> n;
    for (i = 1; i <= n; i++) {
        f >> a[i];
        if (a[i] > bst[k]) {
            bst[++k] = a[i], poz[i] = k;
        }
        else {
            int st = 1, dr = k, mij;
            while (st <= dr) {
                mij = (st+dr)/2;
                if (bst[mij] < a[i] )
                    st = mij+1;
                else dr = mij-1;
            }
            bst[st] = a[i];
            poz[i] = st;
        }
    }
    g << k << '\n';
    for (i = n; i >= 1; i--) {
        if (poz[i] == k)
            sol[++j] = a[i], k--;
    }
    while (j--)
        g << sol[j+1] << ' ';
    return 0;
}