hdu1009 FatMouse' Trade---贪心

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1009

题意:一共有n个房子,每个房子里有老鼠喜欢吃的javabeans,但是每个房间里的javabeans的价格不一样。老鼠用m元,问m元最多可以卖多少javabeans,其中每个房间里的javabeans可以被分割。

直接求单价,排序即可

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<string>
 6 #include<cmath>
 7 #include<set>
 8 #include<queue>
 9 #include<map>
10 #include<stack>
11 #include<vector>
12 #include<list>
13 #include<deque>
14 #include<sstream>
15 #include<cctype>
16 #define REP(i, n) for(int i = 0; i < (n); i++)
17 #define FOR(i, s, t) for(int i = (s); i < (t); i++)
18 #define MEM(a, x) memset(a, x, sizeof(a));
19 #define DEBUG(x) cout<<x<<endl;
20 #define DBG cout<<"----------------"<<endl;
21 #define mid(x, y) x + (y - x)/ 2
22 #define lc o<<1
23 #define rc o<<1|1
24 using namespace std;
25 typedef int64_t INT;
26 typedef long long ll;
27 typedef unsigned long long ull;
28 typedef pair<int, int> Pair;
29 const int maxn = 1e3 + 10;
30 const double eps = 1e-10;
31 const int INF = 1 << 30;
32 const int dir[4][2] = {1,0,0,1,0,-1,-1,0};
33 const double pi = 3.1415926535898;
34 int T, n, m, cases;
35 struct node
36 {
37     double j, f;
38     bool operator < (const node& a)const
39     {
40         return (j / f) > (a.j / a.f);
41     }
42 };
43 node a[maxn];
44 int main()
45 {
46     while(cin >> m >> n)
47     {
48         if(m == -1 && n == -1)break;
49         memset(a, 0, sizeof(a));
50         for(int i = 0; i < n; i++)
51         {
52             cin >> a[i].j >> a[i].f;
53         }
54         sort(a, a + n);
55         double ans = 0;
56         for(int i = 0; i < n; i++)
57         {
58             if(m >= a[i].f)
59             {
60                 ans += a[i].j;
61                 m -= a[i].f;
62             }
63             else
64             {
65                 ans += (m * a[i].j / a[i].f);
66                 break;
67             }
68         }
69         printf("%.3f\n", ans);
70     }
71     return 0;
72 }

 

posted @ 2018-03-29 22:31  _努力努力再努力x  阅读(138)  评论(0编辑  收藏  举报