Cod sursa(job #2375354)

Utilizator Teo_1101Mititelu Teodor Teo_1101 Data 8 martie 2019 08:13:26
Problema Subsir crescator maximal Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int NMAX = 100002;
const int INF = ( 1 << 30 );
int N;
int a[NMAX];
int pre[NMAX];
int LIS[NMAX];
int last;

void Read()
{
    fin>>N;
    for(int i=1;i<=N;++i)
        fin>>a[i];
    fin.close();
}

int BS( int lf, int rg, int val )
{
  if( lf > rg ) return 0;

  int mid = ( lf + rg ) / 2;

  if( val > a[ LIS[mid] ] ) return max( mid, BS( mid + 1, rg, val ) );
  else return BS( lf, mid - 1, val );
}

void Out(int poz)
{
    if(pre[poz]!=0)Out(pre[poz]);
    fout<<a[poz]<<" ";
}
void Lis()
{
    a[0]=-INF;
    a[N+1]=INF;


    LIS[0]=0;
    last=0;

    for(int i=1;i<=N;++i)
        {
            if(a[i]>a[LIS[last]])
                {
                    LIS[++last]=i;
                    pre[i]=LIS[last-1];
                }
            else
            {
                int poz = BS(1,last,i);

                LIS[poz+1]=i;
                pre[i]=LIS[poz];
            }
        }
    fout<<last<<"\n";
    Out(LIS[last]);
}
int main()
{
    Read();
    Lis();
    return 0;
}