Cod sursa(job #1149803)

Utilizator Arodoet96Teodora Stoleru Arodoet96 Data 22 martie 2014 11:44:55
Problema Subsir crescator maximal Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.12 kb
#include <cstdio>
#define DMAX 100002

using namespace std;

FILE*fin=fopen("scmax.in", "r");
FILE*fout=fopen("scmax.out", "w");

int n;
long long int a[DMAX];
int best[DMAX], prec[DMAX];
int lgmax;

void citire();
int caut(int i);
void pd();

int main()
{
    citire();
    pd();
    return 0;
}

void citire()
{
    int i;
    fscanf(fin, "%d", &n);
    for(i=1;i<=n;i++)
        fscanf(fin, "%lld", &a[i]);
}

int caut(int i)
{
    int st=1, dr=lgmax, mij;
    while(st<dr)
    {
        mij=(st+dr)/2;
        if(a[i]>=a[best[mij]])
            dr=mij;
            else st=mij+1;
    }
    return dr;
}

void pd()
{
    int i, p;
    a[0]=2000000010; lgmax=0; best[lgmax]=0;
    for(i=n;i>=1;i--)
        if(a[i]<a[best[lgmax]])
        {
            prec[i]=best[lgmax];
            best[++lgmax]=i;
        }
        else
        {
            p=caut(i);
            prec[i]=best[p-1];
            best[p]=i;
        }
    fprintf(fout, "%d\n", lgmax);
    p=best[lgmax];
    i=lgmax;
    while(p)
    {
        fprintf(fout, "%d ", a[p]);
        p=prec[p];
    }
}