Cod sursa(job #2397477)

Utilizator bilghinIsleam Bilghin bilghin Data 4 aprilie 2019 14:04:33
Problema Subsir crescator maximal Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.85 kb
#include <stdio.h>

int v[100005],d[100005],pre[100005],s[100005];

int main()
{

    FILE* si=fopen("scmax.in","r");
    FILE* so=fopen("scmax.out","w");

    int n,i,l,st,dr,maxx=1,mij;

    fscanf(si,"%d",&n);
    for(i=0; i<n; i++)
    {
        fscanf(si,"%d",&v[i]);
    }
    d[1]=0;
    for(i=1; i<n; i++)
    {
        st=1;
        dr=maxx;
        while(st<=dr)
        {
            mij=(st+dr)/2;
            if(v[d[mij]]<v[i]) st=mij+1;
            else dr=mij-1;
        }
        d[st]=i;
        pre[i]=d[st-1];
        if(st>maxx) maxx=st;
    }
    fprintf(so,"%d \n",maxx);
    int poz=d[maxx];
    for(i=0; i<maxx; i++)
    {
        s[i]=poz;
        poz=pre[poz];
    }
    for(i=maxx-1; i>=0; i--)
    {
        fprintf(so,"%d ",v[s[i]]);
    }

    fclose(si);
    fclose(so);

    return 0;
}