Cod sursa(job #1946049)

Utilizator edi_laitinLaitin Eduard edi_laitin Data 29 martie 2017 21:04:15
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 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],A[100005],pozmax,k;

int BinarySearch(int x)
{
    int st=1;
    int dr=sol+1;
    int poz=sol+1;
    int mid;

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

        if(V[B[mid]]<x)
           {
            st=mid+1;
           }
        else
           {
             dr=mid-1;
             poz=mid;
           }
      }
    return poz;
}

void Solve()
{
    fin>>N;

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

        int poz=BinarySearch(V[i]);

        sol=max(sol,poz);
        B[poz]=i;
        P[B[poz]]=B[poz-1];
      }
}

void Print()
{
   fout<<sol<<"\n";

   int nr=B[sol];

   while(nr)
   {
       A[++k]=nr;
       nr=P[nr];
   }

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

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