您的位置:首页 > 其它

hdu 1009 FatMouse' Trade

2015-08-26 14:18 295 查看

FatMouse' Trade

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 54549 Accepted Submission(s): 18288


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

5 3

7 2
4 3

5 2

20 3

25 18

24 15

15 10

-1 -1

[align=left]Sample Output[/align]

13.333

31.500

[align=left]Author[/align]
CHEN, Yue

[align=left]Source[/align]
ZJCPC2004

[align=left]Recommend[/align]
JGShining | We have carefully selected several similar problems for you: 1051 1052 1007 1006 1053

一道基础的贪心,只需排序就好,运用了sort排序和结构体,感觉十分方便和强大。

题意:老鼠准备了M磅猫食,准备拿这些猫食跟猫交换自己喜欢的食物。有N个房间,每个房间里面都有食物。你可以得到J[i]但你需要付出F[i]的猫食。要你计算你有M磅猫食可以获得最多食物的重量。食物可以被分割。

附上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int a,b;
double c;
}ss[1005];
bool cmp(node x,node y)
{
return x.c>y.c;
}
int main()
{
int n,m,i,j,k,w;
double s;
while(cin>>n>>m)
{
if(n==-1&&m==-1)
break;
s=0;
for(i=0;i<m;i++)
{
cin>>ss[i].a>>ss[i].b;
ss[i].c=(double)ss[i].a/(double)ss[i].b; //求性价比,每元钱可以换多少这种食物
}
sort(ss,ss+m,cmp);  //按性价比大小排序,单价买的越多,性价比越高
for(i=0;i<m;i++)
{
if(n>=ss[i].b)  //拥有的钱超过这个食物全部的价格
{
s+=ss[i].a;  //全部食物的重量
n-=ss[i].b;  //减去花费的钱
}
else   //若不够买这种食物的全部
{
s+=ss[i].c*n;  //花光所有的钱购买这种食物
break;      //钱为0,跳出循环
}
}
printf("%.3lf\n",s);  //保存3位小数输出
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: