PAT B1020/A070 Mooncake

PAT B1020/A070 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 (≤1000), the number of different kinds of mooncakes, and D (≤500 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

参考代码:

 1 /****************************************************
 2 PAT B1020/A070 Mooncake
 3 ****************************************************/
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <vector>
 7 
 8 using namespace std;
 9 
10 struct MoonCake {
11     double storage = 0;
12     double totalPrice = 0;
13     double perPrice = 0;    //单价
14 };
15 
16 bool myCmp(MoonCake a, MoonCake b) { return a.perPrice > b.perPrice; }
17 
18 int main() {
19     double maxProfit = 0;
20     int kinds = 0, demand = 0;
21     
22     cin >> kinds >> demand;
23 
24     vector<MoonCake> MoonCakeInfo(kinds);
25     for (int i = 0; i < kinds; ++i) {
26         cin >> MoonCakeInfo[i].storage;
27     }
28     for (int i = 0; i < kinds; ++i) {
29         cin >> MoonCakeInfo[i].totalPrice;
30 
31         MoonCakeInfo[i].perPrice = MoonCakeInfo[i].totalPrice / MoonCakeInfo[i].storage;
32     }
33 
34     sort(MoonCakeInfo.begin(), MoonCakeInfo.end(), myCmp);
35 
36     for (int i = 0; demand > 0 && i < kinds; ++i) {
37         if (demand < MoonCakeInfo[i].storage) {
38             maxProfit += MoonCakeInfo[i].totalPrice * (demand / MoonCakeInfo[i].storage);
39         }
40         else {
41             maxProfit += MoonCakeInfo[i].totalPrice;
42         }
43 
44         demand -= MoonCakeInfo[i].storage;
45     }
46 
47     printf("%.02f", maxProfit);
48 
49     return 0;
50 }

注意事项:

  1:由题意可知优先卖完剩余月饼中单价最高的可以获得最大的利润。

  2:当n一定大时会超过所有月饼的总和,要注意这种情况。

posted @ 2019-10-12 23:27  多半是条废龙  阅读(121)  评论(0编辑  收藏  举报