Cod sursa(job #1375708)

Utilizator darrenRares Buhai darren Data 5 martie 2015 14:03:25
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.82 kb
#include <cstdio>
#include <algorithm>

using namespace std;

int N;
int A[100002], ST[100002], T[100002];

void track(int x)
{
    if (x == 0) return;
    track(T[x]);

    printf("%d ", A[x]);
}

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

    scanf("%d", &N);
    for (int i = 1; i <= N; ++i)
        scanf("%d", &A[i]);

    for (int i = 1; i <= N; ++i)
    {
        int step = (1 << 16), now = 0;
        for (; step; step >>= 1)
            if (now + step <= ST[0] && A[ST[now + step]] < A[i])
                now += step;
        ++now;

        ST[now] = i;
        if (now != 1)
            T[i] = ST[now - 1];
        else
            T[i] = 0;
        ST[0] = max(ST[0], now);
    }

    printf("%d\n", ST[0]);
    track(ST[ST[0]]);
}