HDOJ----------1009

题目:

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 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
 
分析:本题用到的是贪心算法,猫粮换成Java豆的比例越大应越先被兑换。在此我通过每个结构体保存每个屋子中的J[i]与F[i]及其兑换比例scale,然后利用sort将所有结构体中的scale按从大到小进行排序。之所以这样做的原因是,根据scale排序后,不会打乱原先每个屋子J[i]与F[i]及其兑换比例scale的对应关系,即排序的过程中结构体的结构不发生变换,只不过是根据结构体中的scale变量给所有结构体排一下序而已。最后的换Java豆的工作也简单多了。
 
代码如下:
 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 const int maxN = 1000 + 5;
 6 
 7 struct warehouse{
 8     int J;
 9     int F;
10     double scale;
11 }House[maxN];
12 
13 bool cmp(const struct warehouse a, const struct warehouse b) {
14     return a.scale > b.scale;
15 }
16 
17 int main() {
18     int M, N;
19     double ans;
20     while(scanf("%d %d", &M, &N) == 2){
21         if(M == -1 && N == -1) break;
22         //输入
23         for(int i = 0; i < N; i++) {
24             scanf("%d %d", &House[i].J, &House[i].F);
25             House[i].scale = (double)House[i].J/House[i].F;
26         }
27         //将所有屋子中的猫粮与Java豆兑换的比例排序
28         sort(House, House + N, cmp);
29 //        for(int i = 0; i < N; i++)
30 //            printf("%.3lf\t", House[i].scale);
31         //按比例从大到小分配猫粮
32         ans = 0.0;
33         int pos = 0;
34         while(M > 0 && N > 0){//猫粮换完,或者Java豆已经没有时应该终止循环
35             if(M > House[pos].F)
36                 ans += House[pos].J;        //若猫粮充足,直接将屋子的Java豆兑换下来
37             else
38                 ans += (double)House[pos].J * M / House[pos].F; //能兑换的猫粮不足,这时应该按比例来兑换Java豆
39             M -= House[pos].F;
40             N--;
41             pos++;//到下一家
42         }
43         //输出
44         printf("%.3lf\n", ans);
45     }
46     return 0;
47 }

                                                                2015-07-02文

posted on 2015-07-02 13:12  脚踏实地-仰望星空  阅读(222)  评论(0编辑  收藏  举报

导航