Pagini recente » Cod sursa (job #2180173) | Cod sursa (job #2935930) | Cod sursa (job #2497100) | Cod sursa (job #671916) | Cod sursa (job #440026)
Cod sursa(job #440026)
#include <stdio.h>
#include <stdlib.h>
typedef struct {
unsigned int height;
unsigned int weight;
} fruit;
int compare (const void * a, const void * b)
{
return (((fruit *)b ) ->weight - ((fruit *)a ) ->weight);
}
int main(){
unsigned int h, u, hmin;
int n, i;
fruit *x;
//se citesc datele din fisier
FILE *fin = fopen("gutui.in", "r");
fscanf(fin, "%d", &n);
fscanf(fin,"%u", &h);
fscanf(fin, "%u", &u);
x = (fruit *) malloc( n *sizeof(fruit));
hmin = h;
for(i = 0; i < n; i++){
fscanf(fin, "%u %u", &(x[i].height), &(x[i].weight));
if(x[i].height < hmin)
hmin = x[i].height;
}
fclose(fin);
qsort(x, n, sizeof(x), compare);
unsigned int greutate = 0;
int *cules;
int moment;
cules = (int * ) calloc ((( h - hmin) / u + 2), sizeof (int));
//printf("%d\n",(( h - hmin) / u + 2));
for (i = 0; i < n; i++){
moment = (h - x[i].height) / u;
//printf("%u - %u\n", x[i].height, (h - x[i].height) / u);
while(moment >= 0 && cules[moment]){
moment --;
}
//printf(" mom =%d \n", moment);
if(moment >= 0){
cules[moment] = 1;
greutate += x[i].weight;
}
}
FILE *fout = fopen("gutui.out", "w");
fprintf(fout, "%u", greutate);
fclose(fout);
//printf("greutate : >%d<", greutate);
return 0;
}