Cod sursa(job #1131684)

Utilizator Theorytheo .c Theory Data 28 februarie 2014 23:59:09
Problema Subsir crescator maximal Scor 55
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int NMAX = 100009;

int V[NMAX]; int N; int Best[NMAX]; int last[NMAX]; int start;


int bs(const int& X) {

    int pos = 0; int step = (1 << 17);

    for( ; step ; (step >>= 1))
        if(pos + step <= Best[0] && V[Best[pos + step]] < X)
            pos = pos + step;
    return pos;
}

void print(int X) {

    for(int i = 1; i <= X; ++i)
        fout << Best[i] <<" ";
    fout << '\n';
}

int main() {

    fin >> N;
    for(int i = 1; i <= N; ++i)
        fin >> V[i];
    for(int i = 1; i <= N; ++i) {

        int pos = bs(V[i]);

            last[i] = Best[pos];

        if(V[Best[pos + 1]] > V[i] || Best[pos + 1] == 0) {
            Best[pos + 1] = i;

        }

        if(pos + 1 > Best[0]) {
            Best[0] = pos + 1;
            start = i;
        }

    }

    fout << Best[0] << '\n';
    fout << V[start] << " ";
    while(--Best[0]) {
        fout << V[last[start]] <<" " ;
        start = last[start];
    }

    return 0;
}