首页 > 代码库 > HDU FatMouse' Trade
HDU FatMouse' Trade
FatMouse‘ Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20968 Accepted Submission(s): 6501
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.
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
吐槽一下,竟然不支持lambda表达式...
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 using namespace std; 5 struct WareHouse 6 { 7 int JB,CF; 8 double ratio; 9 };10 bool cmp(WareHouse a,WareHouse b)11 {12 return a.ratio > b.ratio;13 } 14 int main()15 {16 int m,n;17 while(scanf_s("%d%d",&m,&n)&&m!=-1&&n!=-1)18 {19 double answer=0;20 //int*p = new int[n];21 vector<WareHouse> WH;22 int i=n;23 while(i--)24 {25 WareHouse temp;26 scanf_s("%d%d",&temp.JB,&temp.CF);27 temp.ratio = (double)temp.JB/temp.CF;28 WH.push_back(temp);29 }30 // sort(WH.begin(),WH.end(),[](const WareHouse &a,const WareHouse&b ){ return a.ratio>b.ratio;});31 sort(WH.begin(),WH.end(),cmp);32 for (int j = 0; j < n; j++)33 {34 if(m<=WH[j].CF)35 {36 answer=answer+m*WH[j].ratio;37 m=0;38 }39 else40 {41 answer=answer+WH[j].JB;42 m-=WH[j].CF;43 }44 }45 printf("%.3f\n",answer);46 }47 return 0;48 }
HDU FatMouse' Trade
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。