Cod sursa(job #1070488)

Utilizator crisbodnarCristian Bodnar crisbodnar Data 1 ianuarie 2014 12:55:53
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 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];
vector <int> rez;

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

int Query(int poz)
{
    int max = 0;
    while(poz)
    {
        if(best[max] < best[aib[poz]])
            max = aib[poz];
        poz -= (poz ^ (poz - 1)) & 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];
    }

    bool cazu_lu_peste = 1;
    for(int i=1; i<N; i++)
        if(v[i] < v[i+1])
        {
            cazu_lu_peste = 0;
            break;
        }
    if(cazu_lu_peste)
    {
        fout<<"1\n"<<v[1];
        return 0;
    }

    sort(a + 1, a + N + 1);
    for(int i=2; i<=N; i++)
        if(a[top] != a[i])
            a[++top] = a[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;
}