Cod sursa(job #1149765)

Utilizator DuxarFII-Stefan-Negrus Duxar Data 22 martie 2014 11:22:12
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.32 kb
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<cassert>
#include<cstdio>

using namespace std;

int N, lgmax, poz;
vector<int> best, V, next2;

int cautbin(int st, int dr, int val) {
    if (st == dr) {
        assert (st > -1);
        if (V[best[st]] > val) {
            poz = max(poz,st);
        }
        return poz;
    }
    int mij = (st + dr) / 2;
    if (V[best[mij]] > val) {
        poz = max(poz, mij);
        return cautbin(mij + 1, dr, val);
    };
    return cautbin(st, mij - 1, val);
}

int main() {

    freopen("scmax.in", "r", stdin);
    freopen("scmax.out", "w", stdout);
    scanf("%d", &N);
    V.resize(N + 1);
    best.resize(N + 1);
    next2.resize(N);
    best[0] = N;
    V[N] = 1 << 30;
    for (int i = 0; i < N; ++i) {
        scanf("%d", &V[i]);
    }
    for (int i = N - 1; i >= 0; --i) {
        poz = 0;
        cautbin(0, lgmax, V[i]);
        next2[i] = best[poz];
        //best[poz + 1] = max(best[poz + 1], i);
        if (V[best[poz + 1]] < V[i] || best[poz + 1] == 0) {
            best[poz + 1] = i;
        }
        lgmax = max(lgmax, poz + 1);
    }

    poz = best[lgmax];
    cout << lgmax << '\n';
    while (poz != N) {
        printf("%d ", V[poz]) ;
        poz = next2[poz];
    }
    return 0;
}