FatMouse' Trade

http://acm.zju.edu.cn/show_problem.php?pid=2109

Time limit: 1 Seconds

Memory limit: 32768K

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.

1. 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.

2. 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.

3. Sample Input

{{{5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1}}}

4. Sample Output

{{{13.333 31.500}}}


   1 /*Written by czk*/
   2 #include <iostream>
   3 #include <iomanip>
   4 #include <queue>
   5 using namespace std;
   6 struct room{
   7         int java_bean;
   8         int cat_food;
   9         bool operator<(const room &r) const{
  10                 return static_cast<double>(java_bean) / cat_food < static_cast<double>(r.java_bean) / r.cat_food;
  11         }
  12 };
  13 
  14 int main() {
  15         while(1) {
  16                 int m, n;
  17                 cin >> m >> n;
  18                 if (m == -1 && n == -1) break;
  19                 priority_queue<room> rooms;
  20                 for(int i = 0; i < n; i++) {
  21                         room r;
  22                         cin >> r.java_bean >> r.cat_food;               
  23                         rooms.push(r);
  24                 }
  25                 double total = 0.0;
  26                 while(!rooms.empty() && m > 0) {
  27                         if(m>rooms.top().cat_food) {
  28                                 total += rooms.top().java_bean;
  29                                 m -= rooms.top().cat_food;
  30                         } else {
  31                                 total += static_cast<double>(rooms.top().java_bean) / rooms.top().cat_food * m;
  32                                 m = 0;
  33                         }
  34                         rooms.pop();
  35                 }
  36                 cout << fixed<<setprecision(3) << total  << endl;
  37         }
  38 
  39 }

zju2109 (2008-02-23 15:35:02由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.