Cod sursa(job #1673728)

Utilizator alexburdescuBurdescu Alexandru alexburdescu Data 4 aprilie 2016 08:37:22
Problema Subsir crescator maximal Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.05 kb
#include<fstream>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
int N,a[100009],i,Lmax,p1,p2,mij,j,b[100009],c[100009];
int main()
{
    fin>>N;
    for(i=1;i<=N;i++)
    {
        fin>>a[i];
    }

    Lmax=0;
    for(i=N;i>=1;i--)
    {

         p1=1;
         p2=Lmax;
         while(p1<=p2)
         {
             mij=(p1+p2)/2;
             if(b[mij]>a[i])
             {
                 p1=mij+1;
             }
             else
             {
                 p2=mij-1;
             }
         }
         if(p2>=Lmax)
         {
             Lmax++;
             b[Lmax]=a[i];
             c[i]=Lmax;
         }
         else
         {
             c[i]=p2;
             if(b[p2]<a[i])
             {
                 b[p2]=a[i];
             }
         }
    }
    fout<<Lmax;
    fout<<endl;
    for(i=1;i<=N;i++)
    {
        if(c[i]==Lmax)
        {
            fout<<a[i]<<" ";
            Lmax--;
        }
    }
    fin.close();
    fout.close();
    return 0;
}