Cod sursa(job #2398153)

Utilizator AlbertUngureanuAlbert AlbertUngureanu Data 5 aprilie 2019 09:22:02
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>

using namespace std;

int n, v[100003], best[100003], p[100003], maxim, k, L[100003], nr;


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

void afis(int i)
{
   if (p[i] > 0)
        afis(p[i]);
   fout<<v[i]<<" ";
}

int caut(int x)
{
   int p, u, m;
   p = 0;
   u = nr;
   m = (p+u)/2;
   while (p <= u)
   {
      if (v[L[m]] < x &&  v[L[m+1]] >= x)
            return m;
      else if (v[L[m+1]] < x)
            {
                p = m + 1;
                m = (p + u)/2;
            }
            else
                {
                    u = m - 1;
                    m = (p + u)/2;
                }
   }
   return nr;
}

int main()
{
    int i, j, poz;
    nr = 1;
    fin>>n;
    for (i = 1; i <= n; i++)
        fin>>v[i];
    best[1] = L[1] = 1;
    L[0] =  0;
    for (i = 2; i <= n; i++)
    {
        poz = caut(v[i]);
        p[i] = L[poz];
        best[i] = poz + 1;
        L[poz + 1] = i;
    if(nr < poz + 1)
        nr=poz+1;
   }
   maxim = 0;
   poz = 0;
   for (i = 1; i <= n; i++)
       if (maxim < best[i])
       {
          maxim = best[i];
          poz = i;
       }
   fout<<maxim<<'\n';
   afis(poz);
   return 0;
}