Hdoj----------1009

Source: Internet
Author: User

Topic:

Fatmouse ' Trade

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 52127 Accepted Submission (s): 17505


Problem Descriptionfatmouse prepared M pounds of cat food, ready-to-trade with the cats guarding the warehouse containing He favorite food, JavaBean.
The warehouse has N rooms. The i-th contains j[i] pounds of JavaBeans and requires f[i] pounds of cat food. Fatmouse does not has the to trade for all the JavaBeans in the the the same, instead, he may get j[i]* a% of pounds JavaBeans if he Pays f[i]* a% pounds of cat food. Here A is a real number. Now he's 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 is begins with a line containing the non-negative integers M and N. Then N lines follow, each contains, non-negative integers j[i] and f[i] respectively. The last test case was followed by Two-1 ' s. All integers is 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 Amoun T of JavaBeans that Fatmouse can obtain.

Sample INPUT5 37 24 35 220 325 1824 1515 10-1-1

Sample Output13.33331.500 Analysis: The subject is greedy algorithm, cat food into Java beans, the larger the proportion of the more should be redeemed first. Here I save J[i] and f[i in each room with each structure and the scale of the exchange, and then use sort to sort the scales in all the structures from large to small. The reason for this is that, after sorting by scale, it does not disrupt the correspondence between the original house J[i] and the f[i] and their exchange scales, that is, the structure of the structure does not transform during the sequencing process, except that the scale variables of the structure are ordered for all structures. The final switch to Java beans is much easier. The code is as follows:
1#include <cstdio>2#include <algorithm>3 using namespacestd;4 5 Const intMAXN = ++5;6 7 structwarehouse{8     intJ;9     intF;Ten     DoubleScale ; One }HOUSE[MAXN]; A  - BOOLcmpConst structWarehouse A,Const structWarehouse B) { -     returnA.scale >B.scale; the } -  - intMain () { -     intM, N; +     Doubleans; -      while(SCANF ("%d%d", &m, &n) = =2){ +         if(M = =-1&& N = =-1) Break; A         //input at          for(inti =0; i < N; i++) { -scanf"%d%d", &house[i]. J, &House[i]. F); -House[i].scale = (Double) House[i]. j/House[i]. F; -         } -         //sort the ratio of cat food in all rooms to the percentage of Java beans exchanged -Sort (House, House +N, CMP); in //for (int i = 0; i < N; i++) - //printf ("%.3lf\t", House[i].scale); to         //allocate cat food proportionally from large to small +Ans =0.0; -         intpos =0; the          while(M >0&& N >0){//you should stop the cycle if the cat food has been changed or if the Java bean has not been *             if(M >House[pos]. F) $Ans + = House[pos]. J//If you have enough cat food, simply redeem the Java beans from the house.Panax Notoginseng             Else -Ans + = (Double) House[pos]. J * M/house[pos]. F//can redeem the cat food is not enough, then should be proportional to the conversion of Java Beans theM-=House[pos]. F; +n--; Apos++;//to the next house . the         } +         //Output -printf"%.3lf\n", ans); $     } $     return 0; -}

2015-07-02 article

Hdoj----------1009

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.