Cod sursa(job #552315)

Utilizator gerrGabriel Erzse gerr Data 12 martie 2011 09:16:46
Problema Subsir crescator maximal Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 1.33 kb
/* Infoarena SCMAX (Subsir crescator maximal) http://infoarena.ro */
#include <stdio.h>

#define SIZE 110000

int n, a[SIZE];
int len[SIZE], t[SIZE];
int ns, s[SIZE];

void afis(int i)
{
    if (i >= 0) {
        afis(t[i]);
        printf("%d ", a[i]);
    }
}

int main(void)
{
    int i, j, max, jmax, l, r, m;

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

    scanf("%d", &n);
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);

    len[0] = 1;
    t[0] = -1;
    s[1] = 0;
    ns = 1;
    for (i = 1; i < n; i++) {
        l = 1;
        r = ns;
        m = (l + r) / 2;
        while (l <= r) {
            if (m < ns && a[s[m]] < a[i] && a[s[m + 1]] >= a[i])
                break;
            if (a[s[m]] < a[i])
                l = m + 1;
            else
                r = m - 1;
            m = (l + r) / 2;
        }
        if (m == ns || a[s[m + 1]] > a[i]) {
            if (m == ns)
                ns++;
            s[m + 1] = i;
            t[i] = m >= 1 ? s[m] : -1;
            len[i] = ns;
        } else {
            t[i] = -1;
            len[i] = 0;
        }
    }

    max = len[0];
    jmax = 0;
    for (j = 1; j < n; j++) {
        if (max < len[j]) {
            max = len[j];
            jmax = j;
        }
    }
    printf("%d\n", max);
    afis(jmax);
    printf("\n");

    return 0;
}