Cod sursa(job #3239223)

Utilizator mihaigeorgescuGeorgescu Mihai mihaigeorgescu Data 3 august 2024 09:43:40
Problema Subsir crescator maximal Scor 55
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#include <fstream>

using namespace std;
ifstream fcin("scmax.in");
ofstream fout("scmax.out");
int n,v[100001],u[100001],vf,st[100001],next1[100001],vf1,poz;
int cb(int val)
{
    int st=1;
    int dr=vf;
    int mij;
    while(st<=dr)
    {
         mij=(st+dr)/2;
        if(v[u[mij]]>=val && v[u[mij-1]]<val)
        {
            return mij;
        }
        else
        {
            if(val>v[u[mij]])
            {
                st=mij+1;
            }
            else
            {
                dr=mij-1;
            }
        }
    }
    return vf+1;
}
int main()
{
    fcin>>n;
    for(int i=1; i<=n; i++)
        fcin>>v[i];
    vf++;
    u[vf]=1;
   // fout<<u[cb(25)]<<'\n';
    for(int i=2; i<=n; i++)
    {
        int j=cb(v[i]);
     //   fout<<j<<'\n';
        int lg=vf;
        if(j>lg)
        {
            poz=i;
            next1[i]=u[vf];
            vf++;
            u[vf]=i;
        }
        else
        {
            next1[i]=next1[u[j]];
            u[j]=i;
        }
       // for(int j=1; j<=vf; j++)
      //  fout<<u[j]<<" ";
      //  fout<<'\n';
    }
   // fout<<poz<<" ";
  //  for(int i=1; i<=n; i++)
   //     fout<<next[i]<<" ";
   fout<<vf<<'\n';
    while(vf--)
    {
        vf1++;
        st[vf1]=v[poz];
        poz=next1[poz];
    }
    for(int i=vf1; i>=1; i--)
    {
        fout<<st[i]<<" ";
    }
}