Cod sursa(job #2553672)

Utilizator mamacitamamacita mamacita Data 22 februarie 2020 11:00:08
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>
#define NMAX 100002
using namespace std;

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

int n, lgbest;
int a[NMAX];
int best[NMAX];
int poz[NMAX];
int sol[NMAX];
int cautbin(int x);

int main()
{
 int i, j, pozai;
 ///citire
 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<<'\n';
 fin.close();
 fout.close();
 return 0;
}

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