Cod sursa(job #1180586)

Utilizator DenisacheDenis Ehorovici Denisache Data 30 aprilie 2014 19:48:03
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.92 kb
#include <cstdio>
int N,bestL[100005],v[100005],scmax[100005],L=1,poz,maxim,i,tata[100005],pozmax;
int cb(int x)
{
    int Left=0,Right=L,M;
    while (Left<=Right)
    {
        M=(Left+Right)>>1;
        if (v[scmax[M]]<x && v[scmax[M+1]]>=x) return M;
        else if (v[scmax[M+1]]<x) Left=M+1;
        else Right=M-1;
    }
    return L;
}
void afis(int poz)
{
    if (tata[poz]!=0) afis(tata[poz]);
    printf("%d ",v[poz]);
}
int main()
{
   freopen("scmax.in","r",stdin);
   freopen("scmax.out","w",stdout);
   scanf("%d",&N); bestL[1]=scmax[1]=1;
   for (i=1;i<=N;i++)
   {
       scanf("%d",&v[i]);
       if (i>1)
       {
           poz=cb(v[i]);
           bestL[i]=poz+1;
           scmax[poz+1]=i;
           if (L<poz+1) L=poz+1;
           tata[i]=scmax[poz];
       }
       if (maxim<bestL[i]) maxim=bestL[i],pozmax=i;
   }
   printf("%d\n",maxim);
   afis(pozmax);
   return 0;
}