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).
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.
For each test case, print the maximum profit (in billion yuans) in one line, accurate up to 2 decimal places.
3 200
180 150 100
7.5 7.2 4.5
9.45
题意:
根据库存,和这些库存对应的价格,以及所要购买的总量,输出最大收益。
思路:
求出单价,然后对单价进行降序排列,然后购买,直到购得的总量满足要求。(第一次提交的时候有一组测试点没有通过,后来看了别人的blog发现库存也应该用double来表示)
Code:
1 #include
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
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 }
手机扫一扫
移动阅读更方便
你可能感兴趣的文章