Cod sursa(job #1199381)

Utilizator lorundlorund lorund Data 19 iunie 2014 10:33:53
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <cstdio>
#include <vector>
#define SIZE 100005
#define oo 2000000001
using namespace std;

int N;
int lmax, best[SIZE];
int v[SIZE], length[SIZE];
vector<int> sol;

int main()
{
    freopen("scmax.in", "r", stdin);
    freopen("scmax.out", "w", stdout);

    scanf("%d", &N);
    for (int i=0; i<N; ++i){
        int li=1, ls=lmax;
        scanf("%d", &v[i]);

        while (li<=ls){
            int m=(li+ls)/2;

            if (best[m]>=v[i])
                ls = m-1;
            else
                li = m+1;
        }
        if (!best[li]){
            ++lmax;
        }
        best[li] = v[i];
        length[i] = li;
    }
    printf("%d\n", lmax);
    for (int i=N-1, x=oo; lmax; --i)
        if (length[i]==lmax && v[i]<x){
            sol.push_back(v[i]);
            --lmax;
        }
    for (int i=sol.size()-1; i>=0; --i){
        printf("%d ", sol[i]);
    }
    return 0;
}