FatMouse' Trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 77535 Accepted Submission(s): 26592
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题目大意:题目理解应该不难,就是一个猫和老鼠的游戏 ,胖老鼠和猫做交易用猫粮交换老鼠的最爱豆子,有N个房间,房间有两个属性,一个是房间内的豆子,另一个是要想打开这个房间要和猫做交易换的猫粮,现在胖老鼠有M猫粮,问胖老鼠能给老猫换多少豆子,求最大值 ;有N个房间,当然有个a%,刚开始我认为所有房间的a是一样的,后来发现每个房间的a都不一样。那么就开始贪心吧 ;怎么贪心呢,我是先用结构体来存房间比较方便,然后在结构体中加一个属性part,表示每斤猫粮能换多少豆子。然后循环遍历从part最大的开始加和直到猫粮数大于总猫粮,循环结束求出获得的豆子数目;#include<iostream> #include<algorithm> using namespace std; struct Room{ double j_mouse; double p_cat; double part; }; bool cmp(Room x,Room y) { return x.part>y.part; } int main() { int n,m; Room room[1000]; Room temp; int i,j; double j_beads; while (cin>>m>>n) { if(m==-1||n==-1) break; j_beads=0; for(i=0;i<n;i++) { cin>>room[i].j_mouse>>room[i].p_cat; room[i].part=room[i].j_mouse/room[i].p_cat; } sort(room,room+n,cmp); for(i=0;i<n;i++) { if(room[i].p_cat<=m) { j_beads+=room[i].j_mouse; m-=room[i].p_cat; } else { j_beads+=m*room[i].part; break;} } printf("%.3lf\n",j_beads); } return 0; }