Cod sursa(job #1880582)

Utilizator giotoPopescu Ioan gioto Data 15 februarie 2017 20:30:24
Problema Subsir crescator maximal Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <cstdio>
using namespace std;

int n, x, st[100001], pos[100001], a[100001], Sol[100001];
int main()
{
    freopen("scmax.in", "r", stdin);
    freopen("scmax.out", "w", stdout);
    scanf("%d", &n);
    int L = 0;
    for(int i = 1; i <= n ; ++i){
        scanf("%d", &x);
        a[i] = x;
        if(x > st[L]) {pos[i] = L + 1; st[++L] = x;}
        else{
            int mij, p = 1, dr = L;
            while(p <= dr){
                mij = (p + dr) / 2;
                if(st[mij] > x) dr = mij - 1;
                else if(st[mij] < x) p = mij + 1;
                else break;
            }
            if(p <= dr) pos[i] = p;
            else if(p <= L) st[p] = x, pos[i] = mij;
        }
    }
    printf("%d\n", L);
    int aux = L;
    for(int i = n; i >= 1 ; --i)
        if(pos[i] == L) Sol[L--] = a[i];
    for(int i = 1; i <= aux ; ++i)
        printf("%d ", Sol[i]);
    return 0;
}