HDU 1009 FatMouse' Trade
题目大意:
给你\(m\)克猫粮,\(n\)个房间分别可以用猫粮以\(J[i]/F[i]\)的比例换取至多\(J[i]\)克咖啡豆,问最多能换取多少咖啡豆。
题解:
按照性价比排序,总取性价比最大的。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int n, m;
struct Room {
double j, f, rate;
bool operator < (const Room &x) const {
return rate > x.rate;
}
} room[1010];
int main() {
while (~scanf("%d%d", &m, &n)) {
if (n == -1 && m == -1) {
break;
}
for (int i = 1; i <= n; ++i) {
scanf("%lf%lf", &room[i].j, &room[i].f);
room[i].rate = room[i].j / room[i].f;
}
sort(room + 1, room + n + 1);
double ans = 0;
for (int i = 1; i <= n; ++i) {
if (room[i].f <= m) {
ans += room[i].j;
m -= room[i].f;
} else {
ans += room[i].rate * m;
break;
}
}
printf("%.3lf\n", ans);
}
return 0;
}