Cod sursa(job #1673731)

Utilizator alexburdescuBurdescu Alexandru alexburdescu Data 4 aprilie 2016 08:40:12
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 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-1;
    fout<<endl;
    int k=0;
    a[0]=0;
    for(i=1;i<=N;i++)
    {
        if(c[i]==Lmax && a[k]<a[i])
        {
            k=i;
            fout<<a[i]<<" ";
            Lmax--;
        }
    }
    fin.close();
    fout.close();
    return 0;
}