Cod sursa(job #1717340)

Utilizator Burbon13Burbon13 Burbon13 Data 14 iunie 2016 18:38:14
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <cstdio>

using namespace std;

const int nmx = 100002;

int n,t,pmax,valmax = -1;
int a[nmx],sol[nmx],p[nmx];

void rec(int pos)
{
    if(pos)
        for(int i = pos - 1; i; --i)
            if(p[i] + 1 == p[pos])
            {
                rec(i);
                break;
            }
    printf("%d ", a[pos]);
}

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]);

    t = 1;
    sol[1] = a[1];
    p[1] = 1;

    for(int i = 2; i <= n; ++i)
    {
        int st = 1, dr = t, m, ps = 0;
        while(st <= dr)
        {
            int m = (st + dr) / 2;
            if(sol[m] >= a[i] && sol[m-1] < a[i])
                ps = m;
            if(sol[m] < a[i])
                st = m + 1;
            else
                dr = m - 1;
        }

        if(ps == 0)
            ps = ++t;

        sol[ps] = a[i];
        p[i] = ps;
        if(ps > valmax)
        {
            valmax = ps;
            pmax = i;
        }
    }

    printf("%d\n", valmax);
    rec(pmax);

    return 0;
}