Cod sursa(job #2553668)

Utilizator Mihai_Razvan_IonutMihai Razvan Ionut Mihai_Razvan_Ionut Data 22 februarie 2020 10:59:39
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.88 kb
#include <fstream>
#define NMAX 100002


using namespace std;

ifstream fin("scmax.in");
ofstream fout("scmax.out");
int a[NMAX], lgbest;
int best[NMAX];
int poz[NMAX];
int sol[NMAX];
int n;
int cautbin(int x);

int main()
{
    int i, j, pozai;
    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;
    }
    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)
{
 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;
}