1070. Mooncake (25)

﹏ヽ暗。殇╰゛Y 2022-05-30 00:10 242阅读 0赞

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

题目大意:

代码:

  1. #include<stdio.h>
  2. #include<algorithm>
  3. using namespace std;
  4. struct node
  5. {
  6. double amount;
  7. double price;
  8. double unit_price;
  9. }Mooncake[1010];
  10. bool cmp(struct node a,struct node b)
  11. {
  12. return a.unit_price>b.unit_price;
  13. }
  14. int main()
  15. {
  16. int i,j,n,k;
  17. double total,m,sum;
  18. scanf("%d %lf",&n,&m);
  19. for(i=0;i<n;i++)
  20. {
  21. scanf("%lf",&Mooncake[i].amount);
  22. }
  23. for(i=0;i<n;i++)
  24. {
  25. scanf("%lf",&Mooncake[i].price);
  26. Mooncake[i].unit_price=Mooncake[i].price/Mooncake[i].amount;
  27. }
  28. sort(Mooncake,Mooncake+n,cmp);
  29. total=0;
  30. sum=0;
  31. for(i=0;i<n;i++)
  32. {
  33. if(total+Mooncake[i].amount>=m)
  34. {
  35. sum+=(m-total)*(Mooncake[i].unit_price);
  36. total=m;
  37. break;
  38. }
  39. else
  40. {
  41. total+=Mooncake[i].amount;
  42. sum+= Mooncake[i].price;
  43. }
  44. }
  45. printf("%.2lf\n",sum);
  46. return 0;
  47. }

发表评论

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

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

相关阅读

    相关 【杭电1070】Milk

    ![这里写图片描述][20160721084340435] ![这里写图片描述][20160721084352546] 最重要的思想是在输入的时候去掉多余的输入数据。

    相关 1070. 结绳(25)

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