HDU-Fat Mouse' Trade

来源:互联网 发布:vb.net asp网站实例 编辑:程序博客网 时间:2024/05/17 23:14


描述

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 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.

输出

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.

样例输入

5 37 24 35 220 325 1824 1515 10-1 -1

样例输出

13.33331.500

  解析: 小老鼠拿猫食换鼠食,有猫食M,N个屋子,每个屋子中可以用F[i]的猫食换到J[i]的鼠食,离散型贪心过程,所以要对兑换的性价比排序,也就是j[i]/f[i];

  代码:

     

#include<stdio.h>#include<algorithm>using namespace std;struct node {int j,f;double pre;}mouse[3001];bool cmp(node a,node b){return a.pre>b.pre;}int main(){int n,m;while(scanf("%d%d",&m,&n)!=EOF&&(n!=-1||m!=-1)){int i;for(i=0;i<n;i++){scanf("%d%d",&mouse[i].j,&mouse[i].f);mouse[i].pre=(double)mouse[i].j/mouse[i].f;}sort(mouse,mouse+n,cmp);//对兑换的性价比排序;double sum=0;for(i=0;i<n;i++){if(m>mouse[i].f)//兑换条件{sum+=mouse[i].j;//累加可以兑换的老鼠食物;m-=mouse[i].f;//兑换后将其从总数中剔除;}else {sum+=mouse[i].pre*m;//m=0;break;}}printf("%.3lf\n",sum);}}


0 0