Cod sursa(job #1083221)

Utilizator blechereyalBlecher Eyal blechereyal Data 15 ianuarie 2014 19:01:15
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <fstream>
#define MAX_N 100000
using namespace std;

int a[MAX_N]; // vector dat
int start[MAX_N];// start[i]= j unde a[j] este primul element dintr-un subsir crescator de lungime i;
int position[MAX_N];// position[i] = positia elementului dupa a[i] sau -1 daca nu exista
int N,Lmax,i;
int cautare(int first,int last, int value)
{
    int middle;
    while (first<=last)
    {
        middle=first+(last-first)/2;
        if (value<a[start[middle]])
        first=middle+1;
        else
        last=middle-1;
    }
    return first;
}
int main()
{
    ifstream f("scmax.in");
    ofstream g("scmax.out");
    f>>N;

    for (i=1;i<=N;i++)
        f>>a[i];
    a[0]=2000000001;
    for (int i=N;i>=1;i--)
        {
            position[i]=0;
            int lenght=cautare(1,Lmax,a[i]);// caut binar
            if (lenght>Lmax)
            {
                position[i]=start[lenght-1];
                Lmax=lenght;
                start[lenght]=i;
            }
            else{
                position[i]=start[lenght-1];
                if (a[start[lenght]]<a[i])
                 start[lenght]=i;
            }
        }
    g<<Lmax<<"\n";
    for (i=start[Lmax];i>0;i=position[i]){
        g<<a[i]<<" ";
    }

    return 0;
}