Cod sursa(job #901054)

Utilizator ancavanca vlad ancav Data 28 februarie 2013 23:52:41
Problema Subsir crescator maximal Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include<stdio.h>
FILE *f=fopen("scmax.in","r"), *g=fopen("scmax.out","w");
long int n, v[100], best[100], max, maxbest=-1, prec[100],poz,nr=1,s[100];

void citire(){
    fscanf(f,"%ld\n",&n);
    for(int i=1;i<=n;i++){
        fscanf(f,"%ld ",&v[i]);
    }
}

void calc_best(){
best[0]=1;max=-1;
    for(int i=1;i<=n;i++){
        for(int j=0;j<=i-1;j++){
            if(v[j]<v[i]&&v[j]>=max){
                max=v[j];
                best[i]=best[j]+1;
                if(best[i]>maxbest){maxbest=best[i];poz=i;}
                prec[i]=j;
            }
            if(v[j]==v[i]) {best[i]=best[j];}
        }
        if(best[i]==0){best[i]=1;}
    }
}

void construieste_sol(){
    while(nr<=maxbest){
        s[nr]=v[poz];
        nr++;
        poz=prec[poz];
    }
}

int main(){
    citire();
    calc_best();
    construieste_sol();
    for(int i=maxbest-1;i>=1;i--){
        fprintf(g,"%ld ",s[i]);
    }

   // printf("%ld %ld", maxbest, poz );
    return 0;
}