Cod sursa(job #2842815)

Utilizator BarbuceanuConstantinBarbuceanu Constantin BarbuceanuConstantin Data 1 februarie 2022 15:46:30
Problema Subsir crescator maximal Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <stdio.h>   	
int n, v[100003], bestLength[100003], prevIndices[100003], maxim, k, tailIndices[100003], nr;	
void afis(int i)
{
   if (prevIndices[i] > 0)  afis(prevIndices[i]);	
   printf("%d ",v[i]);	
}
int caut(int x)	
{
   int first, last, middle;	
   first = 0; last = nr; middle = (first + last)/2;
   while (first <= last)
   {
      if (v[tailIndices[middle]] < x &&  v[tailIndices[middle + 1]] >= x) return middle;
      else if (v[tailIndices[middle + 1]] < x) {first = middle + 1; middle = (first + last)/2;}
      else {last = middle - 1; middle = (first + last) / 2;}
   }
   return nr;
}
int main()	
{
   freopen("scmax.in","r",stdin);	
   freopen("scmax.out","w",stdout);
   int i, poz;
   nr = 1;

   scanf("%d",&n);
   for (i = 1; i <= n; i++) scanf("%d", v + i);
   
   bestLength[1] = tailIndices[1] = 1;
   for (i = 2; i <= n; i++)
   {
      //caut cel mai mare element mai mic decat v[i]
      //SAU
      //caut cel mai mic element mai mare decat v[i]
      poz = caut(v[i]);
      prevIndices[i] = tailIndices[poz];
      bestLength[i] = poz + 1;
      tailIndices[poz + 1] = i;
      if (nr < poz + 1)   nr = poz + 1;
   }
   
   maxim = 0; poz = 0;
   for (i = 1; i <= n; i++)
       if (maxim < bestLength[i])
       {
          maxim = bestLength[i]; poz = i;
       }
 
   printf("%d\n",maxim);
   afis(poz);
 
   return 0;   
}