Cod sursa(job #1224070)

Utilizator TimitocArdelean Andrei Timotei Timitoc Data 29 august 2014 16:20:57
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#define MAXN 100005

using namespace std;

int n, a[MAXN], stiva[MAXN], best[MAXN], previ[MAXN];
int poz, nr;

void citire()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d", &a[i]);
}

void afisare(int p)
{
    if (p == -1)
        return;
    afisare(previ[p]);
    printf("%d ", a[p]);
}

int place(int x)
{
    int i, step;
    for (step = 1; step < nr; step <<= 1);
    for (i = 0; step; step >>= 1)
        if (i + step < nr && a[stiva[i + step]] <= x)
           i += step;
    return i;
    /*vector<int> v;
    for (int i = 0; i < nr; i++)
        v.push_back(a[stiva[i]]);
    return lower_bound(v.begin(), v.end(), x) - v.begin();*/

}

void sol()
{
    for (int i = 0; i < n; i++)
    {
        poz = place(a[i]);
        stiva[poz] = i;
        best[i] = poz + 1;
        previ[i] = (poz == 0 ? -1 : stiva[poz-1]);
        if (poz + 1 > nr)
            nr = poz + 1;
    }
    int maxim = 0, p = 0;
    for (int i = 0; i < n; i++)
        if (best[i] > maxim)
        {
            maxim = best[i];
            p = i;
        }
    printf("%d\n", maxim);
    afisare(p);
}


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

    citire();
    sol();

    return 0;
}