HDU1009 FatMouse’ Trade

來源:互聯網
上載者: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 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 Input

5 37 24 35 220 325 1824 1515 10-1 -1
 

Sample Output

13.33331.500
 

AuthorCHEN, Yue 

SourceZJCPC2004
 

RecommendJGShining

解題思路:本題為典型貪心演算法題目,由於物品可分割,不能用01背包做。
      先對給出的各個倉庫的資訊(豆子量,所需貓糧量)進行分析,可知倉庫中豆子越多,所需貓糧越少,則越划得來,兌換該房間的豆子可以得到最大的豆子量。
      因此,先求出各個倉庫的豆子量/所需貓糧量的比值(簡稱比值),比值大的應該考慮優先兌換。
      然後將所有倉庫資訊按照比值從大到小排序,得出各倉庫的兌換先後順序,儲存在結構體數組food[]裡面,準備兌換。
      然後按排序結果對相應倉庫進行兌換,若當前所剩貓糧量不為0並且還有倉庫未進行兌換,則繼續兌換,
                (1)如果當前老鼠剩下的貓糧大於兌換當前倉庫所有的豆子的所需的貓糧量,則兌換該倉庫的所有豆子,豆子總量增加該倉庫總豆子量的值,所剩貓糧總量減去兌換當前倉庫所有豆子所需貓糧量;
                (2)如果當前老鼠剩下的貓糧小於兌換當前倉庫所有的豆子的所需的貓糧量,則兌換該倉庫的所有豆子*所剩貓糧/所需的貓糧量,豆子總量增加所有豆子*所剩貓糧/所需的貓糧量(注意精度,這裡的值可能會產生小數),所剩貓糧總量置0;
       最後,按題目要求輸出兌換所得豆子總量(保留3位小數)即可。

#include<stdio.h>#include<algorithm>using namespace std;struct node{    int j;    int f;    double bi;}food[1005]; //兌換情況,豆子量,所需貓糧,豆/貓比bool cmp(node a,node b)    //排序,按比例從大到小排序{    return a.bi>b.bi;}int main(){    int m,n;    int i,j;    while(scanf("%d%d",&m,&n)&&(n!=-1||m!=-1))    {        for(i=0;i<n;i++)        {            scanf("%d%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)   //當貓糧還有,豆子沒有兌換完時,繼續兌換        {            if(m>=food[i].f)  //若當前貓糧能兌換當前倉庫所有豆子,則全部兌換            {                sum+=food[i].j;                m-=food[i].f;            }            else   //若當前貓糧無法兌換當前倉庫所有貓糧,則按比例兌換            {                sum+=(double)m/food[i].f*food[i].j;  //注意精度哦                m=0;  //貓糧用完了            }            i++;  //下一個房間(已按豆/貓比排序)比例不大於已兌換房間,且不小於所有未兌換的房間        }        printf("%.3lf\n",sum);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.