Pagini recente » Cod sursa (job #497845) | Cod sursa (job #96355) | Cod sursa (job #1685197) | Cod sursa (job #2800639) | Cod sursa (job #901050)
Cod sursa(job #901050)
#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;i>=1;i--){
fprintf(g,"%ld ",s[i]);
}
// printf("%ld %ld", maxbest, poz );
return 0;
}