Cod sursa(job #2267125)

Utilizator olaru.andreea12Olaru Andreea olaru.andreea12 Data 23 octombrie 2018 12:06:02
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>
#define NMAX 100002

using namespace std;

ifstream fin("scmax.in");
ofstream fout("scmax.out");

int n, a[NMAX], poz[NMAX], best[NMAX], lgmax, s[NMAX];

void citire()
{
    fin>>n;
    for(int i=1; i<=n; i++)
        fin>>a[i];
}
int cautbin(int x)
//caut binar in best cel mai mic element >= x
//si returnez pozitia lui
{
    int st=0, dr=lgmax+1, mijloc;
    while(dr-st>1)
    {
        mijloc=(st+dr)/2;
        if(best[mijloc]>=x)
            dr=mijloc;
        else
            st=mijloc;
    }
    return dr;
}
void constrbest()
{
    int i, pozitie;
    best[1]=a[1];
    lgmax=1;
    poz[1]=1;
    for(i=2; i<=n; i++)
    {
        if(a[i]>best[lgmax])
            {best[++lgmax]=a[i];
        poz[i]=lgmax;}
        else
        {
            pozitie=cautbin(a[i]);
            best[pozitie]=a[i];
            poz[i]=pozitie;
        }
    }
}

void afisare()
{
    fout<<lgmax<<'\n';
     int cine=lgmax, i;
    for( i=n; i>=1 && cine; i--)
        if(poz[i]==cine)
        {
            s[cine]=a[i];
            cine--;
        }
    for(i=1; i<=lgmax; i++)
        fout<<s[i]<<' ';
    fout<<'\n';
}

int main()
{
    citire();
    constrbest();
    afisare();

    return 0;
}