HDU-1009 FatMouse' Trade
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.
Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
题意:
老鼠有M绑重的猫粮,想和猫换粮食,现在有N个房间,每个房间里有J[I]重的粮需要F[I]的猫粮进行交换,但是不用全部换,只要换到老鼠想要的数量就行,问老鼠怎样能换到最多的粮食
解:求出猫粮与粮食的性价比,再进行排序,从大到小,然后就能求出最大的获利
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include<math.h>
using namespace std;
const int maxn = 1e5+10;
typedef long long ll;
int j[maxn];
int f[maxn];
struct PIONT {
int x, y;
double s;
}p[maxn];
bool cmp(const PIONT &a, const PIONT &b)
{
return a.s>b.s;
}
int main()
{
int n, m;
int cas = 0;
while (~scanf("%d%d", &m, &n) && n>=0 && m>=0) {
//int flag = 0;
for (int i = 1; i <= n; ++i)
{
scanf("%d%d", &p[i].x,&p[i].y);
p[i].s = (1.0 * p[i].x / p[i].y);
}
sort(p+1, p+n+1, cmp);
double sum = 0.0, ans = 0.0;
for (int i = 1; i <= n; ++i)
{
if (sum <= m)
{
if (sum + p[i].y <= m)
{
ans += p[i].x;
sum += p[i].y;
}
else {
ans += p[i].s * (m - sum);//超出了就只求其中的一部分
sum = m;
}
}
else break;
}
printf("%.3f\n", ans);
}
return 0;
}
补题不香吗

浙公网安备 33010602011771号