贪心算法的应用(PAT 1070 Mooncake)

阳光穿透心脏的1/2处 2022-03-28 14:42 364阅读 0赞

贪心法是求解一类最优化问题的方法,它总是考虑局部情况下的最优解

比如在huffman树的构造中,利用贪心算法依次选取数组中权值最大的两个结点组合成新的结点,依次类推

该算法确定全局结果是最优的(每一次决策都是最优的决策)

比如下面这么一道题:

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:

  1. 3 200
  2. 180 150 100
  3. 7.5 7.2 4.5

Sample Output:

  1. 9.45

现在月饼需求量为D,已知n种月饼的库存和总售价,问如何销售这些月饼,使收益能够最大

首先确定贪心策略:“总是选择单价最高的月饼销售,可以获得最大收益”

然后进行决策:

(1)如果当前单价最高的月饼库存量满足需求量,直接全部购买该种月饼即可

(2) 如果库存量无法满足需求量,将该月饼的库存全部买下,需求量减去该月饼库存量,然后进行下一步决策(即下一单价最高月饼能否满足)

编写代码如下:

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <string.h>
  5. using namespace std;
  6. struct moonCake
  7. {
  8. double price; //单价
  9. double store; //总库存量
  10. double sell; //总售价
  11. }moonCakes[1010];
  12. bool cmp(moonCake a, moonCake b) { //将月饼根据单价排序
  13. return a.price > b.price;
  14. }
  15. int main() {
  16. int N;
  17. double M; //N月饼种类,M需求量
  18. double amount = 0.0; //总收益
  19. scanf("%d %lf", &N, &M);
  20. for (int i = 0; i < N; ++i) {
  21. scanf("%lf", &moonCakes[i].store);
  22. }
  23. for (int i = 0; i < N; ++i) {
  24. scanf("%lf", &moonCakes[i].sell);
  25. moonCakes[i].price = moonCakes[i].sell / moonCakes[i].store;
  26. }
  27. sort(moonCakes, moonCakes + N, cmp); //将价格从高到低依次排序
  28. for (int i = 0; i < N; ++i) {
  29. if (moonCakes[i].store > M) { //如果库存大于需求的话
  30. amount += moonCakes[i].price*M;
  31. break;
  32. }
  33. else {
  34. amount += moonCakes[i].sell; //否则直接全拿下
  35. M -= moonCakes[i].store;
  36. }
  37. }
  38. printf("%.2f\n", amount);
  39. system("PAUSE");
  40. return 0;
  41. }

发表评论

表情:
评论列表 (有 0 条评论,364人围观)

还没有评论,来说两句吧...

相关阅读

    相关 PAT~乙级~1070 结绳 ~Python

    目描述: 给定一段一段的绳子,你需要把它们串成一条绳。每次串连的时候,是把两段绳子对折,再如下图所示套接在一起。这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一...