1070 Mooncake
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的更多相关文章
- PAT 1070 Mooncake[一般]
1070 Mooncake (25)(25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Aut ...
- 1070 Mooncake (25 分)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 1070. Mooncake (25)
Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...
- 1070. Mooncake (25)
题目如下: Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many ...
- PAT Advanced 1070 Mooncake (25) [贪⼼算法]
题目 Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many typ ...
- PAT (Advanced Level) 1070. Mooncake (25)
简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vecto ...
- PAT甲题题解-1070. Mooncake (25)-排序,大水题
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- PAT 1070 Mooncake
题目意思能搞成这样我也是服了这个女人了 #include <cstdio> #include <cstdlib> #include <vector> #includ ...
随机推荐
- 记录mysql查询数据遇到的一个小问题
今天在测试的时候,需要使用mysql对插入的数据进行检验,但是写完查询语句的时候执行会报错.原因很简单,这个表名是order(订单),在MySQL语言中order是用来排序的关键字,原则上讲是不能作为 ...
- SpringBoot利用spring.profiles.active=@spring.active@不同环境下灵活切换配置文件
一.创建配置文件 配置文件结构:这里建三个配置文件,application.yml作为主配置文件配置所有共同的配置:-dev和-local分别配置两种环境下的不同配置内容,如数据库地址等. appli ...
- bouncycastle中添加HMAC-SM3支持
一. 最近看完了PKCS#5中的内容,总结一下自己添加HMAC-SM3中遇到的问题和解决方法. 大概通读BC java源码以后,开始上手修改. 在SM3.java中添加如下代码: /** * SM3 ...
- 关于PHP的__construct()函数
1 <?php 2 header("Content-Type:text/html;charset=utf-8");//调整为utf-8编码格式 3 class car 4 { ...
- FreeBSD 12.2 发布
FreeBSD 团队宣布 FreeBSD 12.2 正式发布,这是 FreeBSD 12 的第三个稳定版本. 本次更新的一些亮点: 引入了对无线网络堆栈的更新和各种驱动程序,以提供更好的 802.11 ...
- Kettle连接数据库失败
Kettle是使用Java开发,开源的一款ETL工具,这里记录一下关于Kettle有关于数据库连接的问题 1.mysql版本需要注意区分,在mysql8.0的文档中官方已经给出,使用caching_s ...
- hibernate 的一对多关联关系映射配置
hibernate 是操作实体类: 表是一对多的关系,当创建这2个实体的时候 在一的一方定义一个多的一方的集合 在多的一方定义一个一的一方的对象 表是多对多的关系,当创建这2个实体的时候 在互相中都有 ...
- MyBatis(八):MyBatis插件机制详解
MyBatis插件插件机制简介 MyBatis插件其实就是为使用者提供的自行拓展拦截器,主要是为了可以更好的满足业务需要. 在MyBatis中提供了四大核心组件对数据库进行处理,分别是Exec ...
- Java中的名称命名规范:
Java中的名称命名规范:(不遵守,也不会出现编译的错误) 包名:多单词组成时所有字母都小写:xxxyyyzzz 类名.接口名:多单词组成时,所有单词的首字母大写:XxxYyyZzz 变量名.方法名: ...
- ASP.NET .Core 集成 React SPA 应用
AgileConfig的UI使用react重写快完成了.上次搞定了基于jwt的登录模式(AntDesign Pro + .NET Core 实现基于JWT的登录认证),但是还有点问题.现在使用reac ...