Pagini recente » Cod sursa (job #2527550) | Cod sursa (job #3136081) | Cod sursa (job #2939709) | Clasamentul arhivei de probleme | Cod sursa (job #435365)
Cod sursa(job #435365)
#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 ("gutui2.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);
fclose (in); fclose (out);
return 0;
}