Cod sursa(job #2267050)

Utilizator mihaelacebotarCebotar Mihaela mihaelacebotar Data 23 octombrie 2018 10:32:46
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.38 kb
#include <fstream>
#define NMAX 100002

using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int n, pozitie, lgmax, cine;
int a[NMAX];
int poz[NMAX];
int best[NMAX];
int s[NMAX];
void citire();
void constrbest();
void afisare();
int cautbin(int x);
int main()
{
    citire();
    constrbest();
    afisare();
    return 0;
}
void citire()
{
    int i;
    fin >>n;
    for (i=1; i<=n; i++)
        fin >> a[i];
}
void constrbest()
{
    best[1]=a[1]; lgmax=1; poz[1]=1;
    for(int 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';
    cine=lgmax;
    for (int i=n; i>=1; i--)
    {
        if(poz[i]==cine)
        {
            s[cine]=a[i];
            cine--;
        }
    }
    for(int i=1; i<=lgmax; i++)
        fout << s[i] << ' ';
    fout << '\n';

}
int cautbin(int x)
//caut binar in best cel mai mic elemnt >=i
//si returnesz 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;
}