Cod sursa(job #1375676)

Utilizator darrenRares Buhai darren Data 5 martie 2015 13:56:53
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.63 kb
#include <cstdio>
#include <algorithm>

using namespace std;

int N;
int A[100002], B[100002], pos[100002], wh[100002];
int D[100002];
int AIB[100002], T[100002];
int ST[100002];

inline bool compare(const int& i1, const int& i2)
{
    return A[i1] < A[i2];
}

void update(int x, int pos)
{
    for (; x <= 100000; x += x & -x)
        if (AIB[x] == 0 || D[pos] > D[AIB[x]])
            AIB[x] = pos;
}
int query(int x)
{
    int now = 0;
    for (; x >= 1; x -= x & -x)
        if (AIB[x] != 0 && (now == 0 || D[AIB[x]] > D[now]))
            now = AIB[x];
    return now;
}

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]);
        pos[i] = i;
    }
    sort(pos + 1, pos + N + 1, compare);

    int tm = 0;
    for (int i = 1; i <= N; ++i)
    {
        ++tm;
        wh[tm] = A[pos[i]];

        int now = i;
        while (now <= N && A[pos[i]] == A[pos[now]])
        {
            B[pos[now]] = tm;
            ++now;
        }
        i = now - 1;
    }

    int whres = 0;
    for (int i = 1; i <= N; ++i)
    {
        int wh = i - 1;//query(B[i] - 1);

        D[i] = D[wh] + 1;
        T[i] = wh;

        if (whres == 0 || D[i] > D[whres])
            whres = i;

        update(B[i], i);
    }

    printf("%d\n", D[whres]);
    while (whres)
    {
        ST[++ST[0]] = whres;
        whres = T[whres];
    }
    reverse(ST + 1, ST + ST[0] + 1);

    for (int i = 1; i <= ST[0]; ++i)
        printf("%d ", wh[B[ST[i]]]);
    printf("\n");
}