您的位置:首页 > 其它

杭电1009--FatMouse' Trade

2014-07-14 17:43 330 查看
这是一道简单的贪心题,题意为Fatmouse想要尽可能多得守护自己喜欢的JavaBean,所以,i房间得用F[i]d的cat food换得守护J[i]的JavaBean,当JavaBean不足以换的所有时,则根据所有cat food占所需cat food的比例来乘以i房间JavaBean得到实际能够得到的JavaBean。

思路很简单,要用一定的cat food守护尽可能多得JavaBean,必须要房间的JavaBean多并且需要的cat food 少。所以这种涉及两方面因素的贪心,可以通过两方面的比率来判断优先取哪一个。此题应当优先取J[i]/F[i]大的。


FatMouse' Trade

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 41918 Accepted Submission(s): 13938



Problem Description

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.

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.

Sample Input

5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1


Sample Output

13.333
31.500


Author

CHEN, Yue

Source

ZJCPC2004

Recommend

JGShining | We have carefully selected several similar problems for you: 1050 1051 1003 1052 1010

#include<stdio.h>
#include<algorithm>
using namespace std;
struct cat
{
int j;//javabean
int f;//cat fod
double t;
}B[1005];
typedef struct cat cat;
bool cmp(cat a,cat b)
{
return a.t>b.t;
}
int main()
{
int m,n;
while(scanf("%d%d",&m,&n)!=EOF)
{
int i;
double sum=0;
int k=0;
int xx,yy;
if(m==-1&&n==-1)
break;
for(i=0;i<n;i++)
{
scanf("%d%d",&xx,&yy);
if(yy==0)
sum+=xx;
else
{
B[k].j=xx;//javabean
B[k].f=yy;//cat food(搞清字母含义,不然混乱会加错,我交了两遍都是错在字母搞混含义导致出错)
B[k].t=(double)xx/yy;
k++;
}
}//考虑特殊的当房间不需要cat food时,不需要cat food

sort(B,B+k,cmp);//进行优先排序
//需要考虑几种情况,当n=0,时,当m=0时m和n都不为0时。实际上n=0和m不为0并且n不为0可以归为一类
if(m==0)
printf("%.3lf\n",sum);//直接输出不需要cat food的JavaBean总和
else
{
int temp=m;
for(i=0;i<k;i++)
{
if(temp<=B[i].f)
sum+=B[i].t*(double)temp;
else
sum+=(double)B[i].j;
temp-=B[i].f;
if(temp<=0)
break;
}
printf("%.3lf\n",sum);
}//n=0和m!=0&&n!=0的情况归为一类
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: