1070. Mooncake (25)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=1000), the number of different kinds of mooncakes, and D (<=500 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

思路

简单的贪心算法,求出每种月饼的单价,按单价的从高到低顺序去满足需要的月饼数就可以得到最大利润。

注:当需求超过总月饼时,月饼的索引index应不大于N - 1,也就是说将所有月饼的利润加起来就行,之前没注意检测这一边界条件导致段错误。

代码

#include<iostream>
#include<iomanip>
#include<vector>
#include<algorithm>
using namespace std; class mooncake
{
public:
float amount;
float profit;
float price;
}; bool compare(mooncake a,mooncake b)
{
return a.price > b.price;
} int main()
{
int N;
float demand;
while(cin >> N >> demand)
{
vector<mooncake> cakes(N);
for(int i = ;i < N;i++)
{
cin >> cakes[i].amount;
}
for(int i = ;i < N;i++)
{
cin >> cakes[i].profit;
cakes[i].price = cakes[i].profit/cakes[i].amount;
}
sort(cakes.begin(),cakes.end(),compare);
int index = ;
float sum = ;
while(demand > && index <= N - ) //demand很大时index会越界,要注意判断下。
{
if(demand - cakes[index].amount > )
{
sum += cakes[index].profit;
}
else
{
sum += demand * cakes[index].price;
}
demand -= cakes[index++].amount;
}
cout << fixed << setprecision() << sum << endl;
}
}

PAT1070:Mooncake的更多相关文章

  1. PAT1070. Mooncake (25)

    #include #include #include <stdio.h> #include <math.h> using namespace std; struct SS{ d ...

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

  3. Mooncake (排序+贪心)

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

  4. PAT 1070. Mooncake (25)

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

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

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

  6. 1070. Mooncake (25)

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

  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. [FreeRadius2]遇到问题记录

    在学习FreeRadius2中遇到的问题,和解决. 使用的是2.2 版本,测试的系统是Centos6.7 radtest 没有响应 radiusd 启动正常,测试如下命令不好使 [root@orang ...

  2. 01_MUI之Boilerplate中:HTML5示例,动态组件,自定义字体示例,自定义字体示例,图标字体示例

     1安装HBuilder5.0.0,安装后的界面截图如下: 2 按照https://www.muicss.com/docs/v1/css-js/boilerplate-html中的说明,创建上图的 ...

  3. OSB开发常用资料

    成功搭建OSB环境并运行HelloWorld项目 http://www.beansoft.biz/?p=2066 Oracle Service Bus 11gR1开发环境安装文档 http://www ...

  4. Unix/Linux中的read和write函数

    文件描述符 对于内核而言,所有打开的文件都通过文件描述符引用.文件描述符是一个非负整数.当打开一个现有文件或创建一个新文件时,内核向进程返回一个文件描述符.当读或写一个文件时,使用open或creat ...

  5. SpriteBuilder中的CCSprite9Slice是个什么鬼?

    CCSprite大家都知道,但是加上后面那一串又变成了神马呢? 我们可以首先到官方的API文档网站查一下,如下: http://www.cocos2d-swift.org/docs/api/Class ...

  6. Log4j运用于代码中

    在JAVA代码中,我们要打印输出语句的时候,我们经常会使用System.out.print(),但是在项目开发完后,这些代码就会影响项目的运行效率,所以Log4j就派上用场了.话不多说,直接上代码. ...

  7. css3-------:before和:after的作用

    1.:before和:after的作用就是在指定的元素内容(而不是元素本身)之前或者之后插入一个包含content属性指定内容的行内元素,最基本的用法如下: <!doctype html> ...

  8. Bash里面如何返回绝对路径

    1.返回当前目录的绝对路径: basepath=$(cd `dirname $0`; pwd) echo $basepath 2.返回当前路径的上一级目录: xp_path=`dirname &quo ...

  9. 只需几分钟跟小猫学前端(内含视频教程):nodejs基础之用express、ejs、mongdb建设简单的网站

    开门见山视频教程 https://v.qq.com/x/page/d0645s79xrq.html 前 言: 这是小猫的第二篇node教程,第一篇教程是一个简单的试水,小猫的node教程面向对象为没有 ...

  10. FFmpeg备忘录

    av_dup_packet函数 av_dup_packet会为destruct指针为av_destruct_packet_nofree的AVPacket新建一个缓冲区,将原有的缓冲区数据拷贝至新缓冲区 ...