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. C#语言特性及发展史

    本文按照C#语言的发展历史,介绍C#每个版本的新增特性,主要参考微软官方文档.了解这些语言特性可以帮助我们更高效的编写C#代码. C# 1.0 与Visual Studio .NET 2002一起发布 ...

  2. 第43天学习打卡(JVM探究)

    JVM探究 请你谈谈你对JVM的理解?Java8虚拟机和之前的变化更新? 什么是OOM,什么是栈溢出StackOverFlowError? 怎么分析? JVM的常用调优参数有哪些? 内存快照如何抓取, ...

  3. Flask:处理Web表单

    尽管 Flask 的请求对象提供的信息足以处理 Web 表单,但有些任务很单调,而且要重复操作.比如,生成表单的 HTML 代码和验证提交的表单数据.Flask-WTF 扩展可以把处理 Web 表单的 ...

  4. 剑指 Offer 22. 链表中倒数第k个节点

    剑指 Offer 22. 链表中倒数第k个节点 Offer 22 常规解法 常规解法其实很容易可以想到,只需要先求出链表的长度,然后再次遍历取指定长度的链接即可. package com.walega ...

  5. Linux速通06 系统的初始化服务和监控

    Linux系统引导的顺序 # 掌握 Linux系统引导的顺序 * BIOS的工作是检查计算机的硬件设备,如CPU.内存和风扇速度等 * MBR会在启动盘的第一个块中,大小为512B,其中前446B是引 ...

  6. Windows常用快捷键和基本dos命令

    Windows常用快捷键 键盘功能键:Tab,Shift,Ctrl,Alt,空格,Enter,Window... 键盘快捷键: 全选:Ctrl+A 复制: Ctrl+C 粘贴: Ctrl+V 撤销: ...

  7. 漏洞复现-Discuz-命令执行(wooyun-2010-080723)

            0x00 实验环境 攻击机:win10 靶机:Ubuntu18 (docker搭建的vulhub靶场) 0x01 影响版本 Discuz 7.x 6.x版本 0x02 实验目的 学习d ...

  8. Linux系统用户与用户组管理

    一.用户和用户组的管理 1.新增组 groupadd 命令 格式:groupadd 组名 2.删除组 groupdel 格式:groupdel 组名 3.增加用用户命令 useradd   格式:us ...

  9. P1092 虫食算 题解(搜索)

    题目链接 P1092 虫食算 解题思路 好题啊!这个搜索好难写...... 大概是要考虑进位和考虑使用过某个数字这两个东西,但就很容易出错...... 首先这个从后往前搜比较好想,按照从后往前出现的顺 ...

  10. android分析之Parcel

    将数据打包,跨进程传输(通过Binder).看看这货究竟是啥玩意: Parcel.java : public final class Parcel { private static final boo ...