1070 Mooncake

Posted ruruozhenhao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1070 Mooncake相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

1070 Mooncake

1070. Mooncake (25)

1070 Mooncake 贪心

1070 Mooncake

PAT甲题题解-1070. Mooncake (25)-排序,大水题

1070 Mooncake (25 分)难度: 简单 / 知识点: 贪心