hdu 1009 FatMouse' Trade - 贪心

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
Recommend
JGShining
(转自hdu)

   按照f / j排序
Code
 1 /**
 2  * acm.hdu.edu.cn
 3  * Problem#1009
 4  * Accepted
 5  * Time:46ms
 6  * Memory:1596k
 7  */
 8 #include<iostream>
 9 #include<cstdio>
10 #include<cctype>
11 #include<cstring>
12 #include<cstdlib>
13 #include<cmath>
14 #include<fstream>
15 #include<sstream>
16 #include<algorithm>
17 #include<map>
18 #include<set>
19 #include<queue>
20 #include<vector>
21 #include<stack>
22 using namespace std;
23 typedef bool boolean;
24 #define INF 0xfffffff
25 #define smin(a, b) a = min(a, b)
26 #define smax(a, b) a = max(a, b)
27 template<typename T>
28 inline void readInteger(T& u){
29     char x;
30     int aFlag = 1;
31     while(!isdigit((x = getchar())) && x != '-');
32     if(x == '-'){
33         x = getchar();
34         aFlag = -1;
35     }
36     for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
37     ungetc(x, stdin);
38     u *= aFlag;
39 }
40 
41 typedef class Data{
42     public:
43         int f;
44         int j;
45         double b;
46 }Data;
47 
48 int n, m;
49 Data* datas;
50 
51 inline boolean init() {
52     readInteger(n);
53     readInteger(m);
54     if(n == -1 && m == -1)    return false;
55     datas = new Data[(const int)(m + 1)];
56     for(int i = 1; i <= m; i++){
57         readInteger(datas[i].j);
58         readInteger(datas[i].f);
59         datas[i].b = datas[i].j * 1.0 / datas[i].f;
60     }
61     return true;
62 }
63 
64 boolean cmpare(const Data& a, const Data& b){
65     return a.b > b.b;
66 }
67 
68 double result;
69 inline void solve() {
70     result = 0.0;
71     sort(datas + 1, datas + m + 1, cmpare);
72     int i = 1;
73     while(n >= 0 && i <= m){
74         int buy = min(n, datas[i].f);
75         if(buy == datas[i].f)    result += datas[i].j;
76         else    result += buy * 1.0 / datas[i].f * datas[i].j;
77         n -= buy;
78         i++;
79     }
80     printf("%.3lf\n", result);
81 }
82 
83 int main(){
84     while(init()){
85         solve();
86         delete[] datas;
87     }
88     return 0;
89 }
posted @ 2016-11-15 20:38  阿波罗2003  阅读(308)  评论(0编辑  收藏  举报