Cod sursa(job #2402406)

Utilizator vladadAndries Vlad Andrei vladad Data 10 aprilie 2019 17:54:01
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda excelenta-tema5 Marime 0.94 kb
#include<bits/stdc++.h>
using namespace std;
ifstream f("scmax.in");
ofstream g("scmax.out");
int n, v[100001], a[100001], b[100001], c[100001], nr=1, maxim, poz;
void afis(int i)
{
   if (b[i] > 0)  afis(b[i]);
   g<<v[i]<<' ';
}
int caut(int x)
{
   int p, u, m;
   p = 0; u = nr; m = (p+u)/2;
   while (p <= u)
   {
      if (v[c[m]] < x && v[c[m+1]] >= x) return m;
      else if (v[c[m+1]] < x) {p = m + 1; m = (p + u)/2;}
      else {u = m - 1; m = (p + u)/2;}
   }
   return nr;
}
int main()
{
    f>>n;
    for(int i=1; i<=n; i++)
        {
            int x;
            f>>v[i];
        }
    a[1]=c[1]=1;
    c[0]=0;
    for(int i=2; i<=n; i++)
    {
        poz=caut(v[i]);
        b[i]=c[poz];
        a[i]=poz+1;
        c[poz+1]=i;
        nr=max(nr, poz+1);
    }
    poz=0;
    for(int i=1; i<=n; i++)
        if(maxim<a[i]) maxim=a[i], poz=i;
    g<<maxim<<'\n';
    afis(poz);
    return 0;
}