Cod sursa(job #1926968)

Utilizator GandalfTheWhiteGandalf the White GandalfTheWhite Data 14 martie 2017 20:36:45
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include <algorithm>
#include <cstdio>
#define N 100005
using namespace std;

int best[N],p[N],l[N],n,nr=1,maxim,pos,V[N];

void Read(){

    int x,i;
    freopen("scmax.in","r",stdin);

    scanf("%d",&n);
    for (i=1;i<=n;++i)
        scanf("%d",&V[i]);

}

inline int BinSearch(int x){

    int hi=nr,lo=0,mid=(lo+hi)/2;

    while (lo<=hi)
        if (V[l[mid]]<x && V[l[mid+1]]>=x) return mid;
        else if (V[l[mid+1]]<x) {
                                lo=mid+1;
                                mid=(lo+hi)/2;
                                }
             else {
                  hi=mid-1;
                  mid=(lo+hi)/2;
                  }
    return nr;
}

void Dynamic(){

    int i,j;

    l[1]=best[1]=nr=1;
    for (i=2;i<=n;++i) {

        pos=BinSearch(V[i]);
        p[i]=l[pos];
        best[i]=pos+1;
        l[pos+1]=i;
        nr=max(nr,pos+1);
        }

    for (i=1;i<=n;++i)
        if (maxim<best[i]) {
                            maxim=best[i];
                            pos=i;
                           }
}

void afis(int i){

    if (p[i]>0) afis(p[i]);
    printf("%d ",V[i]);
}

void Write(){

    int i;
    freopen("scmax.out","w",stdout);

    printf("%d\n",maxim);
    afis(pos);

}
int main()
{
    Read();
    Dynamic();
    Write();
    return 0;
}