Cod sursa(job #1354371)

Utilizator Theodor1000Cristea Theodor Stefan Theodor1000 Data 21 februarie 2015 19:48:20
Problema Subsir crescator maximal Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.39 kb
#include <cstdio>
#include <algorithm>

#define f first
#define s second
#define sol(x) (x&(-x))

using namespace std;

int v[100010], b[100010], aib[100010], c[100010], prv[100010], poz[100010], n, p;
pair <int, int> a[100010];

inline int query (int x)
{
    int rez = 0;
    for (; x; x -= sol(x))
        if (aib[x] > rez) rez = aib[x], p = poz[x];

    return rez + 1;
}

inline void add (int x)
{
    int y = x + sol(x);
    for (; y <= n; y += sol(y))
        aib[y] = max (aib[y], aib[x]);
}

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

    scanf ("%d\n", &n);

    for (int i = 1; i <= n; ++i)
    {
        scanf ("%d", &v[i]);
        a[i].f = v[i];
        a[i].s = i;
    }

    sort (a + 1, a + n + 1);

    int x = 0;
    for (int i = 1; i <= n; ++i)
    {
        if (a[i].f != a[i - 1].f) ++x;
        b[x] = v[a[i].s];
        v[a[i].s] = x;
    }

    int ma = 0, p1;
    for (int i = 1; i <= n; ++i)
    {
        p = 0;
        aib[v[i]] = query (v[i] - 1);
        poz[v[i]] = i;
        prv[i] = p;
        if (aib[v[i]] > ma) ma = aib[v[i]], p1 = i;
        add (v[i]);
    }

    int k = 0;
    for (int i = p1; i; i = prv[i])
        c[++k] = v[i];

    printf ("%d\n", k);

    for (int i = k; i; --i)
        printf ("%d ", b[c[i]]);

    printf ("\n");

    return 0;
}