Cod sursa(job #1963024)

Utilizator MarianConstantinMarian Constantin MarianConstantin Data 12 aprilie 2017 11:18:37
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.53 kb
#include <iostream>
#include <fstream>

using namespace std;

int a[100010], poz[100010], t[100010], b[100010], c[100010];

int main()
{
    ifstream fin ("scmax.in");
    ofstream fout ("scmax.out");
    int n, i, j, max, x=0, k=0, mij, li, lf, aux;
    fin >> n;
    for (i=1; i<=n; i++)
         fin >> a[i];
    for (i=1; i<=n; i++)
    {
        if (i==1)
        {
            b[++x]=a[i];
            poz[x]=i;
        }
        else
        {
            if (a[i]>b[x])
            {
                b[++x]=a[i];
                poz[x]=i;
                t[i]=poz[x-1];
            }
            else
            {
                li=1;
                lf=x;
                mij=(li+lf)/2;
                while (li<=lf && b[mij]!=a[i])
                {
                    if (a[i]<b[mij])
                        lf=mij-1;
                    else
                        li=mij+1;
                    mij=(li+lf)/2;
                }
                if (b[mij]==a[i])
                {
                    poz[mij]=i;
                    t[i]=poz[mij-1];
                }
                else
                {
                    b[li]=a[i];
                    poz[li]=i;
                    t[i]=poz[li-1];
                }
            }
        }
    }
    fout << x << "\n";
    aux=poz[x];
    while (t[aux])
        {
            c[++k]=a[aux];
            aux=t[aux];
        }
        c[++k]=a[aux];
        for (i=k; i>=1; i--)
            fout << c[i] << " ";
    return 0;
}