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:

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

Sample Output:

  1. 9.45

题意:

  根据库存,和这些库存对应的价格,以及所要购买的总量,输出最大收益。

思路:

  求出单价,然后对单价进行降序排列,然后购买,直到购得的总量满足要求。(第一次提交的时候有一组测试点没有通过,后来看了别人的blog发现库存也应该用double来表示)

Code:

  1. 1 #include <bits/stdc++.h>
  2. 2
  3. 3 using namespace std;
  4. 4
  5. 5 struct Mooncake {
  6. 6 double amount = 0.0;
  7. 7 double profit = 0.0;
  8. 8 double price = 0.0;
  9. 9 };
  10. 10
  11. 11 bool cmp(Mooncake a, Mooncake b) { return a.price > b.price; }
  12. 12
  13. 13 int main() {
  14. 14 int n, total;
  15. 15 cin >> n >> total;
  16. 16 vector<Mooncake> v(n);
  17. 17 for (int i = 0; i < n; ++i) cin >> v[i].amount;
  18. 18 for (int i = 0; i < n; ++i) {
  19. 19 cin >> v[i].profit;
  20. 20 v[i].price = v[i].profit / v[i].amount;
  21. 21 }
  22. 22 sort(v.begin(), v.end(), cmp);
  23. 23 double sumProfit = 0.0;
  24. 24 for (int i = 0; i < n; ++i) {
  25. 25 if (total >= v[i].amount) {
  26. 26 sumProfit += v[i].profit;
  27. 27 total -= v[i].amount;
  28. 28 } else {
  29. 29 sumProfit += v[i].profit * ((double)total / v[i].amount);
  30. 30 break;
  31. 31 }
  32. 32 }
  33. 33 cout << fixed << setprecision(2) << sumProfit << endl;
  34. 34 return 0;
  35. 35 }

1070 Mooncake的更多相关文章

  1. PAT 1070 Mooncake[一般]

    1070 Mooncake (25)(25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Aut ...

  2. 1070 Mooncake (25 分)

    1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn ...

  3. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

  4. PAT 1070. Mooncake (25)

    Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival.  Many types ...

  5. 1070. Mooncake (25)

    题目如下: Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many ...

  6. PAT Advanced 1070 Mooncake (25) [贪⼼算法]

    题目 Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many typ ...

  7. PAT (Advanced Level) 1070. Mooncake (25)

    简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vecto ...

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

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  9. PAT 1070 Mooncake

    题目意思能搞成这样我也是服了这个女人了 #include <cstdio> #include <cstdlib> #include <vector> #includ ...

随机推荐

  1. HashSet为什么可以有序输出?

    首先HashSet是不保证有序,而不是保证无序,因为在HashSet中,元素是按照他们的hashCode值排序存储的.对于单个字符而言,这些hashCode就是ASCII码,因此,当按顺序添加自然数或 ...

  2. SSRF攻击原理

    目录 什么是SSRF 原理 防护 什么是SSRF 一个对外的Web接口,改接口能让用户控制curl命令,去访问别的web服务. 简图如下 想象一下当用户请求的baidu.com/x.php?image ...

  3. 全局解决Vue跳转相同路由导致报错的问题

    大家使用Vue做开发的时候应该都遇到过这个问题,就是某个页面下调用this.$router.push(path),而path指向的页面和当前页面是同一页面时,就会发生报错,vue-router会提示你 ...

  4. 剑指 Offer 36. 二叉搜索树与双向链表 + 中序遍历 + 二叉排序树

    剑指 Offer 36. 二叉搜索树与双向链表 Offer_36 题目描述 题解分析 本题考查的是二叉树的中序遍历以及二叉排序树的特征(二叉排序树的中序遍历序列是升序序列) 利用排序二叉树中序遍历的性 ...

  5. Java数组:多维数组(二维),Arrays类,冒泡排序

    Arrays类数组工具类:java.util.ArraysArrays类中的方法都是static修饰的静态方法,在使用的时候可以直接使用类名进行调用,而"不用"使用对象来调用 具有 ...

  6. python-实现顺序栈

    7 class sqstack(object): 8 def __init__(self,maxsize): 9 self.maxsize = maxsize 10 self.stackElem = ...

  7. Linux+mysql混杂

    一.linux 1.linux中给某一文件中批量新增一个内容 先vim进入文件,然后先按ctrl+v 然后选中需要的行数, 在shift+i 写你要添加的东西 然后在按下esc 二,mysql 1.l ...

  8. java.lang.IllegalArgumentException: MALFORMED

    java.lang.IllegalArgumentException: MALFORMED at java.util.zip.ZipCoder.toString(ZipCoder.java:58) a ...

  9. J. Cole 的 InnoDB 系列 - 1. 学习 InnoDB - 深入探索核心原理之旅

    原文地址:https://blog.jcole.us/2013/01/02/on-learning-innodb-a-journey-to-the-core/,本系列翻译会在其基础上扩展一些 MySQ ...

  10. 【Makefile】2-Makefile的介绍及原理

    目录 前言 概念 Chapter 2:介绍 2.1 makefile的规则 2.3 make 是如何工作的 ** 2.5 让 make 自动推导 2.8 Makefile 里面有什么 2.9 Make ...