Cod sursa(job #1070481)

Utilizator crisbodnarCristian Bodnar crisbodnar Data 1 ianuarie 2014 12:42:45
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>

#define fiu1 (nod << 1)
#define fiu2 (fiu1 + 1)
#define mid ((st + dr) >> 1)

using namespace std;

ifstream fin("scmax.in");
ofstream fout("scmax.out");

const int Nmax = 100010;

int N, top = 1, sol, v[Nmax], best[Nmax], t[Nmax], a[Nmax], aib[Nmax];

void Update(int poz, int i)
{
    while(poz <= N)
    {
        if(best[i] > best[aib[poz]])
            aib[poz] = i;
        poz += (poz&-poz);
    }
}

int Query(int poz)
{
    int max = 0;
    while(poz)
    {
        if(best[max] < best[aib[poz]])
            max = aib[poz];
        poz -= (poz&-poz);
    }
    return max;
}

void Afisare(int poz)
{
    if(!poz) return;
    Afisare(t[poz]);
    fout<<v[poz]<<' ';
}

int main()
{
    fin>>N;
    for(int i=1; i<=N; i++)
    {
        fin>>v[i];
        a[i] = v[i];
    }

    sort(a+1, a+N+1);
    for(int i=2; i<=N; i++)
        if(a[top] != v[i])
            a[++top] = v[i];

    for(int i=1; i<=N; i++)
    {
        int poz = lower_bound(a+1, a+top+1, v[i]) - a;
        t[i] = Query(poz - 1);
        best[i] = best[t[i]] + 1;
        Update(poz, i);
        if(best[sol] < best[i])
            sol = i;
    }

    fout<<best[sol]<<'\n';
    Afisare(sol);

    return 0;
}