Cod sursa(job #2371381)

Utilizator victorv88Veltan Victor victorv88 Data 6 martie 2019 17:24:24
Problema Ubuntzei Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <iostream>
#include <fstream>
using namespace std;

ifstream f("scmax.in");
ofstream g("scmax.out");

int n, val[100005], ultima_pozitie=0, pozitii[100005];
int vect_crescator[100005];

int cb(int x)
{
    int st=1, dr=ultima_pozitie;
    while (st<=dr)
    {
        int mij=(st+dr)/2;
        if (vect_crescator[mij]>=x)
        {
            dr=mij-1;
        }
        else
        {
            st=mij+1;
        }
    }
    if (st==ultima_pozitie+1)
        return -1;
    return st;
}

void afisare_recursiv(int poz, int n)
{
    if (poz==0)
        return;
    for (int i=n; i>0; --i)
    {
        if (pozitii[i]==poz)
        {
            afisare_recursiv(poz-1,i-1);
            g <<val[i] <<' ';
            return;
        }
    }
}

int main()
{
    f >> n;
    for (int i=1; i<=n; ++i)
    {
        f >> val[i];
        int poz=cb(val[i]);
        if (poz==-1)
        {
            vect_crescator[++ultima_pozitie]=val[i];
            pozitii[i]=ultima_pozitie;
        }
        else
            {
                vect_crescator[poz]=val[i];
                pozitii[i]=poz;
            }
    }
    g << ultima_pozitie << '\n';
    afisare_recursiv(ultima_pozitie,n);
    return 0;
}