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:

3 200
180 150 100
7.5 7.2 4.5
 

Sample Output:

9.45

题意:

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

思路:

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

Code:

 1 #include <bits/stdc++.h>
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<Mooncake> v(n);
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 }

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. Redis持久化机制 RDB和AOF的区别

    一.简单介绍 Redis中的持久化机制是一种当数据库发生宕机.断电.软件崩溃等,数据库中的数据无法再使用或者被破坏的情况下,如何恢复数据的方法. Redis中共有两种持久化机制 RDB(Redis D ...

  2. MySQL:初识数据库

    对于大型互联网公司来说,数据的重要性可能比软件本身更重要.据说淘宝业务系统的开发大概消耗约7000万人民币,而其保存的用户数据的价值远高于此,加上数据分析算法的加持,其产生的价值和收益无疑是巨大的,远 ...

  3. 在不使用外延层的同轴半绝缘衬底材料上制作4H-SIC横向双重注入金属氧化物半导体场效应晶体管

    在不使用外延层的同轴半绝缘衬底材料上制作4H-SIC横向双重注入金属氧化物半导体场效应晶体管 杂志:日本应用物理杂志   在不使用外延层在同轴的半绝缘SIC衬底上制作4H-SIC横向双重注入金属氧化物 ...

  4. 使用 SVG transform rotate 解决画框中的数字跟随旋转的问题

    问题描述 在图片上画框标注数字,旋转画布后,数字随之旋转,可读性不强,要求修改成无论画布怎么旋转,数字都是正向显示~ 原交互图示: 解决方案 先看下 dom 的结构 然后看下下面简单的代码 // 获取 ...

  5. docker 部署mysql服务之后,执行sql脚本

    1,先将.sql文件copy到docker容器里 docker ps //找到容器的短ID或者指定的name. docker inspect  -f '{{.Id}}' id or name 得到指定 ...

  6. C# 应用 - 多线程 5) 死锁

    两个线程中的每一个线程都尝试锁定另外一个线程已锁定的资源时,就会发生死锁. 两个线程都不能继续执行. 托管线程处理类的许多方法都提供了超时设定,有助于检测死锁. 例如,下面的代码尝试在 lockObj ...

  7. 对Java异常的理解

    JAVA小白手写总结 提示:本篇简单列举了一些Java中的异常与异常处理 前言 提示:很多小伙伴们都会问到,什么是异常呢,又该怎么解决呢? 下面我们就从下面的一个案例中切入今天的内容. 提示:以下是本 ...

  8. 2019 GDUT Rating Contest I : Problem C. Mooyo Mooyo

    题面: C. Mooyo Mooyo Input file: standard input Output file: standard output Time limit: 1 second Memory ...

  9. python多线程参考文章

    1. https://www.jianshu.com/p/c93e630d8089 2.https://www.runoob.com/python/python-multithreading.html ...

  10. CSS篇-dispaly、position、定位机制、布局、盒子模型、BFC

    display常用值 参考链接英文参考链接中文 // 常用值 none:元素不显示 inline:将元素变为内联元素,默认 block:将元素变为块级元素 inline-block:将元素变为内联块级 ...