Cod sursa(job #1237985)

Utilizator RaileanuCristian Raileanu Raileanu Data 5 octombrie 2014 13:26:52
Problema Subsir crescator maximal Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <fstream>
#include<algorithm>
using namespace std;
ifstream f1("scmax.in");
ofstream f2("scmax.out");
#define MX 100005

int n,m, v[MX],lst[MX],up[MX],D[MX], AIB[MX],RES[MX],bst ;

void update(int x, int ind)
{
    for (; x <= n; x += x^(x-1) & x)
        if (D[ind] > D[AIB[x]])
            AIB[x] = ind;
}

int query(int x)
{
    int mx = 0;

    for (; x; x -= x^(x-1) & x)
        if (D[AIB[x]] > D[mx])
            mx = AIB[x];
    return mx;
}

int main()
{   int i, h=1;
    f1>>n;
    for (i=1; i<=n; i++)
    {   f1>>v[i];
        lst[i]=RES[i]=v[i]; }

    sort(lst+1,lst+n+1);

    for (i=2; i<=n; i++)
        if (lst[h]!=lst[i])
            lst[++h]=lst[i];

    for (i=1; i<=n; i++)
        v[i]=lower_bound(lst+1,lst+n+1,v[i])-lst;

    for (i = 1; i <= n; ++i)
    {
        up[i] = query(v[i]-1);
        D[i] = D[up[i]] + 1;
        update(v[i], i);
    }

    for (i=1; i<=n; i++)
        if (D[i]>D[bst] ) bst=i;

    for (i=bst, h=D[bst] ; i>0; i=up[i], h-- )
        lst[h]=RES[i];

    f2<<D[bst]<<"\n";
    for (i=1; i<=D[bst]; i++)
        f2<<lst[i]<<" ";

    f2.close();
    return 0;
}