Cod sursa(job #1680417)

Utilizator SlevySlevoaca Stefan-Gabriel Slevy Data 8 aprilie 2016 18:59:57
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream in("scmax.in");
ofstream out("scmax.out");
const int NMAX = 100005;
int v[NMAX],best[NMAX],L[NMAX],p[NMAX];
int n;
int bLength;
char buff[NMAX];
int ps = 0;

void Read(int &a)
{
    while(!isdigit(buff[ps]))
        if(++ps == NMAX)
            in.read(buff,NMAX),ps = 0;
    a = 0;
    while(isdigit(buff[ps]))
    {
        a = a*10 + buff[ps] - '0';
        if(++ps == NMAX)
            in.read(buff,NMAX),ps = 0;
    }
}

int binar(int val)
{
    int x = 0,y = bLength;
    int m;
    while(x<=y)
    {
        m = (x+y)/2;
        if(v[L[m]] < val && v[L[m+1]] >= val)
            return m;
        else
        {
            if(v[L[m]] >= val)
                y = m - 1;
            else
                x = m + 1;
        }
    }
    return bLength;
}

void print(int index)
{
    if(p[index] != 0)
        print(p[index]);
    out<<v[index]<<" ";
}

int main()
{
    Read(n);
    for(int i=1;i<=n;i++)
        Read(v[i]);
    in.close();
    bLength = 1;
    L[0] = 0;
    L[1] = 1;
    best[1] = 1;
    int best_l,maxx = 1,pos = 1;
    for(int i=2;i<=n;i++)
    {
         best_l = binar(v[i]);
         best[i] = best_l + 1;
         L[best_l+1] = i;
         p[i] = L[best_l];
         if(bLength < best_l + 1)
            bLength = best_l + 1;
         if(best[i] > maxx)
         {
             maxx = best[i];
             pos = i;
         }
    }
    out<<maxx<<"\n";
    print(pos);
    out.close();
    return 0;
}