Cod sursa(job #2158113)

Utilizator MarianConstantinMarian Constantin MarianConstantin Data 10 martie 2018 10:40:29
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#include <iostream>
#include <fstream>

using namespace std;

int a[100010], b[100010], poz[100010], t[100010], x=0, st, dr, mij, k, n;

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