PAT_A1070#Mooncake
Source:
Description:
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
Keys:
- 贪心
- ven要用double存储,int存储精度会有误差
Code:
/*
Data: 2019-07-22 19:35:47
Problem: PAT_A1070#Mooncake
AC: 13:45 题目大意:
给出各种月饼的库存和价格,和市场需求总量,求最大利润
输入:
第一行给出,月饼种类N<=1e3,市场需求总量D<=500
第二行给出,库存量
第三行给出,价格
输出:
最大利润(两位小数) 基本思路:
根据贪心算法,总是优先选择单价最高月饼
*/
#include<cstdio>
#include<algorithm>
using namespace std;
const int M=1e3+;
struct node
{
double ven,prs;
}mcake[M]; bool cmp(const node &a, const node &b)
{
return a.prs/a.ven > b.prs/b.ven;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n,d;
scanf("%d%d", &n,&d);
for(int i=; i<n; i++)
scanf("%lf", &mcake[i].ven);
for(int i=; i<n; i++)
scanf("%lf", &mcake[i].prs);
sort(mcake,mcake+n,cmp);
double ans=;
for(int i=; i<n; i++)
{
if(d > mcake[i].ven)
{
ans += mcake[i].prs;
d -= mcake[i].ven;
}
else if(d <= mcake[i].ven)
{
ans += d*(mcake[i].prs/mcake[i].ven);
break;
}
}
printf("%.2f\n", ans); return ;
}
PAT_A1070#Mooncake的更多相关文章
- HDU 4122 Alice's mooncake shop 单调队列优化dp
Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...
- Mooncake (排序+贪心)
Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...
- PAT 1070. Mooncake (25)
Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...
- hdu 4122 Alice's mooncake shop(单调队列)
题目链接:hdu 4122 Alice's mooncake shop 题意: 有n个订单和可以在m小时内制作月饼 接下来是n个订单的信息:需要在mon月,d日,year年,h小时交付订单r个月饼 接 ...
- 1070. Mooncake (25)
题目如下: Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many ...
- PAT1070:Mooncake
1070. Mooncake (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mooncake is ...
- A1070. Mooncake
Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...
- HDU 4122 Alice's mooncake shop (RMQ)
Alice's mooncake shop Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- PAT 1070 Mooncake[一般]
1070 Mooncake (25)(25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Aut ...
随机推荐
- Hive 窗口函数
举例: row_number() over(partition by clue_id order by state_updated desc) 业务举例: select distinct a.clue ...
- 用 Flask 来写个轻博客 (7) — (M)VC_models 的关系(many to many)
目录 目录 前文列表 扩展阅读 前期准备 多对多 使用样例 一直在使用的 session 前文列表 用 Flask 来写个轻博客 (1) - 创建项目 用 Flask 来写个轻博客 (2) - Hel ...
- yum安装LAMP
安装LAMP环境二进制包安装,先更新yum源,PHP 7.0.33 扩展可选 yum -y install mysql mysql-server mysql-devel httpd httpd-dev ...
- c# Winform实现发送邮件
邮件发送类 来源网上 稍作调整...出处忘了 /** * 命名空间: EmailSend * 类 名: EmailSend * * 作者 变更内容 变更日期 * ─ ...
- redis demo
方法hset(String key,String field,String value),hmset(String key, Map<String,String> hash),hgetAl ...
- 好947 Mybatis 配置resultMap 带參数查询Map 注意selectOne数据库返回结果一条数据库 否则会报错
//TMD 写几个demo 还有大站採集 <a target=_blank href="http://hao947.com/" target="_blank&quo ...
- Java中date和calendar的用法
获取现在系统的时间和日期看起来是一件非常神奇的事情,但是当使用date和calendar之后发现仍然非常神奇. 1.date 使用date日期之前需要导入包: import java.text.Sim ...
- js两个数组去重后,绑定控件,并支持模糊搜索数组项以及数组互移
设计大概是这个样子的,很简单,两个div,两个互移按钮,一个搜索框,要求搜索框输入时,触发待选框的搜索项 <input class="form-control" placeh ...
- ES模块的基本用法常见使用问题
本文作者:高峰,360奇舞团前端工程师,W3C WoT工作组成员. ES6中引入了模块(Modules)的概念,相信大家都已经挺熟悉的了,在日常的工作中应该也都有使用. 本文会简单介绍一下ES模块的优 ...
- Github Statistics 一个基于 React 的 GitHub 数据统计工具
V 站曾经有个热帖说为何我的开源项目只有 Fork 没有 Star,楼下有个热评说开源项目关注的不应该是 Commit 数据吗?先不论 Star.Fork 和 Commit,issue .pr 也应是 ...