Cod sursa(job #440026)

Utilizator rurryRuxandra Nistor rurry Data 11 aprilie 2010 21:31:27
Problema Gutui Scor 0
Compilator c Status done
Runda teme_upb Marime 1.23 kb
#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;
}