Cod sursa(job #1657453)

Utilizator DrumeaVDrumea Vasile DrumeaV Data 20 martie 2016 15:06:26
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.31 kb
#include <stdio.h>
#include <algorithm>

using namespace std;

const int Mn = 1e5 + 6;

int n, ans;
int a[Mn], b[Mn], bit[Mn], dp[Mn], last[Mn], pos[Mn];

int update(int x, int id)
{
    for (; x <= n; x += x ^ (x - 1) & x)
        if (dp[id] > dp[bit[x]])
           bit[x] = id;
}

int query(int x)
{
    int sol = 0;
    for (; x; x -= x ^ (x - 1) & x)
        if (dp[bit[x]] > dp[sol])
           sol = bit[x];

    return sol;
}

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]);
        b[i] = a[i];
    }

    int it = 1;
    sort(b + 1, b + 1 + n);
    for (int i = 2; i <= n; i++)
        if (b[it] != b[i])
           b[++it] = b[i];

    for (int i = 1; i <= n; i++)
        pos[i] = lower_bound(b + 1, b + it + 1, a[i]) - b;

    for (int i = 1; i <= n; i++)
    {
        last[i] = query(pos[i] - 1);
        dp[i] = dp[last[i]] + 1;
        update(pos[i], i);
    }

    for (int i = 1; i <= n; i++)
        if (dp[ans] < dp[i])
           ans = i;

    it = 0;
    for (int i = ans; i; i = last[i])
        b[++it] = a[i];

    printf("%d\n", dp[ans]);
    for (int i = it; it; printf("%d ", b[it--]));

    printf("\n");
    return 0;
}