Pagini recente » Cod sursa (job #722480) | Cod sursa (job #1200025) | Cod sursa (job #2775201) | Cod sursa (job #771744) | Cod sursa (job #436070)
Cod sursa(job #436070)
#include <stdio.h>
//#include <conio.h>
#include <string.h>
#include <stdlib.h>
FILE* fs;
FILE* fd;
int N, max_height, height_inc,i,j,x,max_weight=0,increase=0,rest,min,k, minmin, kk;
typedef struct {
int height;
int weight;
int getIt;
} gutuie;
gutuie tree[100001];
int compare (const void * a, const void * b)
{
if( ( (*(gutuie*)a).height - (*(gutuie*)b).height ) == 0 )
return ( (*(gutuie*)a).weight - (*(gutuie*)b).weight );
else return ( (*(gutuie*)a).height - (*(gutuie*)b).height );
}
int main(){
fs = fopen("gutui.in","r");
fd = fopen("gutui.out","w");
if (fs==NULL) {
printf("Eroare");
exit (1);
}
if (fd==NULL) {
printf("Eroare");
exit (1);
}
fscanf(fs,"%i",&N);
fscanf(fs,"%i",&max_height);
fscanf(fs,"%i",&height_inc);
rest = max_height%height_inc;
max_height=max_height/height_inc;
//printf("salut");
for(i=0;i<N;i++){
fscanf(fs,"%i",&x);
if(x%height_inc>rest)
tree[i].height = x / height_inc + 1;
else
tree[i].height = x / height_inc;
fscanf(fs,"%i",&x);
tree[i].weight = x;
tree[i].getIt=0;
}
//for(i=0;i<N;i++)
// printf("%i %i \n", tree[i].height, tree[i].weight);
qsort(tree,N,sizeof(gutuie),compare);
//for(i=0;i<N;i++)
// printf("%i %i %i\n", tree[i].height, tree[i].weight, tree[i].getIt);
minmin = tree[N-1].weight; kk=0;
for(i=N-1;i>=0;i--){
if((tree[i].height+increase)<=max_height){
increase++;
tree[i].getIt=1;
if(tree[i].weight<minmin){
minmin=tree[i].weight;
kk=i;
}
}
else
if(tree[i].weight > minmin){
tree[kk].getIt=0;
tree[i].getIt=1;
min=tree[i].weight;
k=i;
for(j=N-1;j>i;j--)
if(tree[i].height!=tree[j].height)
if( tree[j].weight<min && tree[j].getIt==1){
min=tree[j].weight;
k=j;
}
minmin = min;
kk=k;
}
}
for(i=0;i<N;i++)
printf("%i %i %i\n", tree[i].height, tree[i].weight, tree[i].getIt);
for(i=0;i<N;i++) if(tree[i].getIt==1) max_weight+=tree[i].weight;
fprintf(fd,"%i",max_weight);
fclose(fs);
fclose(fd);
//getch();
}