Source:

PAT A1070 Mooncake (25 分)

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的更多相关文章

  1. 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 ...

  2. Mooncake (排序+贪心)

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

  3. PAT 1070. Mooncake (25)

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

  4. hdu 4122 Alice's mooncake shop(单调队列)

    题目链接:hdu 4122 Alice's mooncake shop 题意: 有n个订单和可以在m小时内制作月饼 接下来是n个订单的信息:需要在mon月,d日,year年,h小时交付订单r个月饼 接 ...

  5. 1070. Mooncake (25)

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

  6. PAT1070:Mooncake

    1070. Mooncake (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mooncake is ...

  7. A1070. Mooncake

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

  8. 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 ...

  9. PAT 1070 Mooncake[一般]

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

随机推荐

  1. SSH的两种登录方式

    ssh客户端使用的是Xshell,windows环境. 第一种方式,用户名密码方式 原理如下: 客户端发起ssh请求之后,服务器把自己的公钥传给客户端 客户端输入服务器密码通过公钥加密之后传给服务器 ...

  2. Dubbo入门到精通学习笔记(二十):MyCat在MySQL主从复制的基础上实现读写分离、MyCat 集群部署(HAProxy + MyCat)、MyCat 高可用负载均衡集群Keepalived

    文章目录 MyCat在MySQL主从复制的基础上实现读写分离 一.环境 二.依赖课程 三.MyCat 介绍 ( MyCat 官网:http://mycat.org.cn/ ) 四.MyCat 的安装 ...

  3. ubuntu php多版本共存切换

    做开发时,由于本机开发的php版本跟线上发布的php版本不一致,很容易在上线后,发现因版本的影响导致一些bug,但又不想重新去换本机的php版本,那么多版本共存就很方便了!有必要时,切换到指定版本测试 ...

  4. LeetCode N皇后 & N皇后 II

    题目链接:https://leetcode-cn.com/problems/n-queens/ 题目链接:https://leetcode-cn.com/problems/n-queens-ii/ 题 ...

  5. Neo4j parameter

    Neo4j browser: $ :help param Set a parameter Set a parameter to be sent with queries. The :param nam ...

  6. MyBatis笔记一:GettingStart

    MyBatis笔记一:GettingStart 1.MyBatis优点 我们的工具和各种框架的作用就是为了我们操作数据库简洁,对于一些数据库的工具能帮我们少写一些处理异常等等的代码,但是他们并不是自动 ...

  7. JNI Hello World

    1.什么是JNI:               JNI(Java Native Interface):java本地开发接口               JNI是一个协议,这个协议用来沟通java代码和 ...

  8. 【LeetCode】排序

    [349] Intersection of Two Arrays [Easy] 两个无序可重复数组找交集, 交集要求元素唯一. Given nums1 = [1, 2, 2, 1], nums2 =  ...

  9. position: absolute 如果不设置left, right, top, bottom会在什么位置

    一般我们设置position: absolute都会一起设置left/right/top/bottom, 但是如果不设置, 布局会是什么样子的呢? 直接上图 1.一个大盒子中4个小盒子, 正常文档流布 ...

  10. nodejs模块——fs模块 使用fs.read读文件

    使用fs.read读文件 fs.read() 先介绍fs.open. fs.open(path,flags,[mode],callback)方法用于打开文件,以便fs.read()读取. 参数说明: ...