Cod sursa(job #1944715)

Utilizator edi_laitinLaitin Eduard edi_laitin Data 29 martie 2017 11:03:02
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.07 kb
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

ifstream fin("scmax.in");
ofstream fout("scmax.out");

int N,sol,V[100005],P[100005],B[100005],pozmax,k;

void Solve()
{
    fin>>N;

    for(int i=1;i<=N;i++)
    {
        fin>>V[i];

        int st=1;
        int dr=sol+1;
        int poz=sol+1;
        int mid;

        while(st<=dr)
        {
            mid=(st+dr)/2;

            if(B[mid]<V[i])
            {
              st=mid+1;
              P[i]=mid;
            }
            else
            {
              dr=mid-1;
              poz=mid;
              P[i]=mid;
            }
        }
      if(poz>sol)
            {
              sol++;
              pozmax=poz;
            }
       B[poz]=V[i];
    }
    memset(V,0,sizeof(V));

    V[1]=B[sol];
   k=2;
   fout<<sol<<"\n";

   while(pozmax>=1)
   {
       V[k++]=B[P[pozmax]];
       pozmax=P[pozmax];
   }

   for(int i=sol;i>=1;i--)
   {
       fout<<V[i]<<" ";
   }
}

int main()
{
    Solve();
    return 0;
}