Fatmouse 'trade
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 43381 accepted submission (s): 14499
Problem descriptionfatmouse 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.
Inputthe 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.
Outputfor 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 input5 37 24 35 220 325 1824 10-1-1
Sample output13.33331.500
Authorchen, Yue greedy question: fatmouse has m-pound cat food, it wants to use these cat food for JavaBean, then there are n rooms, each room has a corresponding JavaBean and how much cat food is needed. Q: How many an can I get at most for M-pound cat food?
First, sort the data in a descending order based on the cost-effectiveness ratio (that is, dividing the JavaBean by cat food. Note that the cost-effectiveness ratio may be decimal places). Then the problem is solved.
1 # include <iostream> 2 # include <cstdio> 3 # include <algorithm> 4 # include <iomanip> 5 using namespace STD; 6 # define n 1005 7 8 struct trade {9 Int J, F; // J represents JavaBeans, and f Represents cat food10 double price; // cost-effective 11} trade [N]; 12 13 int CMP (trade a, trade B) 14 {15 return. price> B. price; 16} 17 int main () 18 {19 double maximum; 20 int m, n, I; 21 While (CIN> m> N) 22 {23 maximum = 0; 24 if (M =-1 & n =-1) 25 break; 26 maximum = 0; 27 for (I = 0; I <n; I ++) 28 {29 CIN> Trade [I]. j> Trade [I]. f; 30 trade [I]. price = Trade [I]. J * 1.0/trade [I]. f; 31} 32 sort (trade, trade + N, CMP); 33 for (I = 0; I <n; I ++) 34 {35 if (M = 0) 36 break; 37 Else if (Trade [I]. F <= m) 38 {39 maximum + = Trade [I]. j; 40 m-= Trade [I]. f; 41} 42 else43 {44 maximum + = m * Trade [I]. price; 45 m = 0; 46} 47} 48 printf ("%. 3lf \ n ", maximum); 49} 50 return 0; 51}
View code