Cod sursa(job #435370)

Utilizator lucia.rosculeteLucia Rosculete lucia.rosculete Data 7 aprilie 2010 12:53:23
Problema Gutui Scor 10
Compilator cpp Status done
Runda teme_upb Marime 1.01 kb
#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;

typedef struct height_weight_fruit {
	int height;
	int weight;
} fruit;


bool compare_fruits (fruit f1, fruit f2) {
	if (f1.height == f2.height)
		return (f1.weight > f2.weight);	
	return (f1.height > f2.height);
}

int main() {
	FILE *in, *out;
	in = fopen ("gutui.in", "r");
	out = fopen ("gutui.out", "w");
	
	int n, h, u;
	fruit *quince;
	
	fscanf (in, "%d %d %d", &n, &h, &u);
	quince = (fruit *)calloc(n, sizeof(fruit));
	for (int i=0; i<n; i++) {
		fscanf (in, "%d %d", &(quince[i].height), &(quince[i].weight));
	}
	
	vector<fruit> fruits (quince, quince+n);
	vector<fruit>::iterator it;

	sort (fruits.begin(), fruits.end(), compare_fruits);
	int sum=0;
	for (it=fruits.begin(); it!=fruits.end(); it++) {
		//printf("%d %d \n", (*it).height, (*it).weight);
		//if (h<0)
		//	break;
		if ((*it).height <= h) {
			sum += (*it).weight;
			h -= u;
		}
	}
	fprintf(out, "%d", sum);
	//printf("%d", sum);
	fclose (in); fclose (out);
	return 0;
}