码迷,mamicode.com
首页 > 其他好文 > 详细

1070 Mooncake

时间:2020-05-03 21:57:42      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:rate   sts   ons   根据   blog   note   bak   cli   sample   

Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types of fillings and crusts can be found in traditional mooncakes according to the region‘s culture. Now given the inventory amounts and the prices of all kinds of the mooncakes, together with the maximum total demand of the market, you are supposed to tell the maximum profit that can be made.

Note: partial inventory storage can be taken. The sample shows the following situation: given three kinds of mooncakes with inventory amounts being 180, 150, and 100 thousand tons, and the prices being 7.5, 7.2, and 4.5 billion yuans. If the market demand can be at most 200 thousand tons, the best we can do is to sell 150 thousand tons of the second kind of mooncake, and 50 thousand tons of the third kind. Hence the total profit is 7.2 + 4.5/2 = 9.45 (billion yuans).

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N (≤), the number of different kinds of mooncakes, and D (≤ thousand tons), the maximum total demand of the market. Then the second line gives the positive inventory amounts (in thousand tons), and the third line gives the positive prices (in billion yuans) of N kinds of mooncakes. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the maximum profit (in billion yuans) in one line, accurate up to 2 decimal places.

Sample Input:

3 200
180 150 100
7.5 7.2 4.5
 

Sample Output:

9.45

题意:

  根据库存,和这些库存对应的价格,以及所要购买的总量,输出最大收益。

思路:

  求出单价,然后对单价进行降序排列,然后购买,直到购得的总量满足要求。(第一次提交的时候有一组测试点没有通过,后来看了别人的blog发现库存也应该用double来表示)

Code:

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 struct Mooncake {
 6     double amount = 0.0;
 7     double profit = 0.0;
 8     double price = 0.0;
 9 };
10 
11 bool cmp(Mooncake a, Mooncake b) { return a.price > b.price; }
12 
13 int main() {
14     int n, total;
15     cin >> n >> total;
16     vector<Mooncake> v(n);
17     for (int i = 0; i < n; ++i) cin >> v[i].amount;
18     for (int i = 0; i < n; ++i) {
19         cin >> v[i].profit;
20         v[i].price = v[i].profit / v[i].amount;
21     }
22     sort(v.begin(), v.end(), cmp);
23     double sumProfit = 0.0;
24     for (int i = 0; i < n; ++i) {
25         if (total >= v[i].amount) {
26             sumProfit += v[i].profit;
27             total -= v[i].amount;
28         } else {
29             sumProfit += v[i].profit * ((double)total / v[i].amount);
30             break;
31         }
32     }
33     cout << fixed << setprecision(2) << sumProfit << endl;
34     return 0;
35 }

 

1070 Mooncake

标签:rate   sts   ons   根据   blog   note   bak   cli   sample   

原文地址:https://www.cnblogs.com/ruruozhenhao/p/12823506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!