Cod sursa(job #489647)

Utilizator purdea.andreiPurdea Andrei purdea.andrei Data 3 octombrie 2010 09:20:57
Problema Subsir crescator maximal Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 0.98 kb
/* Implemented: October 3 2010
   It took 23 minutes and 14 seconds to get it right */
#include <stdio.h>
#define S 100010
int n, L, X[S], M[S], P[S], R[S];

int bsearch(int val) {
    if (L==0) return 0;
    int i = 1, s = 0;
    while (i<=L) i<<=1;
    i>>=1;
    while (i) {
        if (((s | i) <= L) && X[M[s | i]]<val) s|=i;
        i>>=1;
    }
    if (X[M[s]]<val) return s;
    return 0;
}

int main() {
    int i, j;

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

    scanf("%d", &n);  for (i=0;i<n;i++) scanf("%d", X + i);
    
    L=0;
    for (i=0;i<n;i++) {
        j = bsearch(X[i]);
        D printf("j: %d\n", j);
        P[i] = M[j];
        if (j == L || X[M[j+1]] > X[i]) {
            M[j+1] = i;
            if (j+1>L) L = j+1;
        }
    }
    j = M[L];
    for (i=0;i<L;i++) {
        R[i] = X[j];
        j = P[j];
    }
    printf("%d\n", L);
    for (i=L-1;i>=0;i--) {
        printf("%d ", R[i]);
    }
    printf("\n");
    return 0;
}