HDU1009 FatMouse 'trade

Source: Internet
Author: User

FatMouse 'trade
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 33703 Accepted Submission (s): 10981

 

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


Solution: this topic is a typical greedy algorithm. Because items are segmented, the 001 backpack cannot be used.
First, we analyze the information of each warehouse (the amount of beans, the amount of cat food required). We can see that the more beans in the warehouse, the less cat food required, the more we get, redeem the beans in the room to get the maximum amount of beans.
Therefore, first obtain the ratio of the beans/required cat food in each warehouse (abbreviated as the ratio). If the ratio is large, the exchange should be prioritized.
Sort all the Warehouse Information in ascending order according to the ratio to obtain the redemption order of each warehouse, store it in the structure array food [], and prepare for redemption.
Then, redeem the corresponding warehouse according to the sorting result. If the remaining cat food volume is not 0 and the warehouse is not exchanged, continue to redeem,
(1) If the remaining cat food for the current mouse is greater than the amount of cat food required to redeem all the beans in the current Warehouse, all the beans in the warehouse will be exchanged, the total number of beans increases the total number of beans in the warehouse, and the total number of remaining cat foods minus the amount of cat food required to redeem all beans in the current warehouse;
(2) If the remaining cat food for the current mouse is less than the amount of cat food required to redeem all the beans in the current Warehouse, the remaining cat food/The amount of cat food required in the warehouse, the total number of beans increases the total number of all beans * the remaining cat food/the required cat food (note the precision. The value here may produce decimals), and the total number of remaining cat food is set to 0;
Finally, output the total amount of beans (retain 3 decimal places) as required by the question.


 

Include <stdio. h ># include <algorithm> using namespace std; struct node {int j; int f; double bi;} food [1005]; // exchange information, beans, required cat food, beans/cats are more ordered than bool cmp (node a, node B) // sort by proportion from large to small {return. bi> B. bi;} int main () {int m, n; int I, j; while (scanf ("% d", & m, & n) & (n! =-1 | m! =-1) {for (I = 0; I <n; I ++) {scanf ("% d", & food [I]. j, & food [I]. f); food [I]. bi = (double) food [I]. j/food [I]. f ;}sort (food, food + n, cmp); double sum = 0; I = 0; while (m & I <n) // when there is a cat grain, when the beans are not fully exchanged, continue to redeem {if (m> = food [I]. f) // if the current cat food can be exchanged for all beans in the current Warehouse, {sum + = food [I]. j; m-= food [I]. f;} else // if the current cat food cannot be exchanged for all cat food in the current Warehouse, {sum + = (double) m/food [I] in proportion. f * food [I]. j; // pay attention to the accuracy. m = 0; // the cat food is used up} I ++; // The next room (sorted by bean/CAT ratio) the proportion is not greater than the value of the converted room, and not less than the value of all rooms not convertible} printf ("%. 3lf \ n ", sum);} return 0 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.