zoj 2109 FatMouse' Trade

FatMouse' Trade

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

 分析:贪心法,找性价比最高的房间。

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 using namespace std;
 5 typedef struct fm{
 6     int j, f;
 7     double aver;
 8 } room;
 9 
10 bool cmp(const room &a, const room &b){
11     return a.aver > b.aver;
12 }
13 
14 int main(){
15     int m, n;
16     cout.precision(3);
17     vector<room> v;
18     while(cin >> m >> n){
19         if(m == -1 && n == -1)
20             break;
21         v.clear();
22         for(int i = 0; i < n; i++){
23             room rm;
24             v.push_back(rm);
25             cin >> v[i].j >> v[i].f;
26             v[i].aver = double(v[i].j) / v[i].f;
27         }
28         sort(v.begin(), v.end(), cmp);
29         double sum = 0;
30         for(int i = 0; i < n; i++){
31             if(m > v[i].f){
32                 sum += v[i].j;
33                 m -= v[i].f;
34             } else {
35                 sum += (double)m * v[i].j / v[i].f;
36                 break;
37             }
38         }
39         cout << fixed << sum << endl;
40     }
41     return 0;
42 }

 

posted @ 2017-03-09 20:55  琴影  阅读(271)  评论(0编辑  收藏  举报