Cod sursa(job #2554205)

Utilizator george-rotariuRotariu George george-rotariu Data 22 februarie 2020 17:46:56
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <fstream>
#define NMAX 100002

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

int a[NMAX];
int best[NMAX];
int poz[NMAX];
int sol[NMAX];
int n, i, j, pozai, lgbest;

int cautbin (int x);
int main()
{
   fin>>n;
   for (i=1; i<=n; i++) fin>>a[i];
   best[1]=a[1]; poz[1]=1; lgbest=1;
   for (i=2; i<=n; i++)
       if (a[i]>best[lgbest])
          {best[++lgbest]=a[i]; poz[i]=lgbest;}
          else
          {
           pozai=cautbin(a[i]);
           best[pozai]=a[i];
           poz[i]=pozai;
          }
   ///afisare
   fout<<lgbest<<'\n';
   for (j=n, i=lgbest; i>0; j--)
       if (poz[j]==i) {sol[i]=a[j]; i--;}
   for (i=1; i<=lgbest; i++)
       fout<<sol[i]<<' ';
   fout.close();
   return 0;
}

int cautbin (int x)
{
 ///caut cel mai mic element mai mare sau egal cu x in vectorul best de la 1 la lgbest
 int st=0, dr=lgbest, mij;
 while (dr-st>1)
       {
        mij=(st+dr)/2;
        if (best[mij]<x)
           st=mij;
           else
           dr=mij;
       }
 return dr;
}