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

此题没有什么难度,就是一个贪心算法,或者说是一个分数背包问题,唯一需要注意的是题目中不能讲amounts的类型设为int,要设为double,不然有一个测试点过不了的,PAT就是一个坑啊。
#include <vector>
#include <algorithm>
#include <cstdio>
#include <functional>
using namespace std; const int NUM=; struct Mooncake
{
double amounts;
double prices;
double rate;
Mooncake()
{
amounts=0.0;
prices=0.0;
rate=0.0;
}
}; class Greater:public binary_function<Mooncake,Mooncake,bool>
{
public:
bool operator()(const Mooncake& lhs,const Mooncake& rhs) const
{
return lhs.rate>rhs.rate;
}
}; vector<Mooncake> mooncakes;
int main()
{
int N,D;
double amounts,prices;
Mooncake buf;
scanf("%d%d",&N,&D);
int i=;
for(;i<N;++i)
{
scanf("%lf",&amounts);
buf.amounts=amounts;
mooncakes.push_back(buf);
}
for(i=;i<N;++i)
{
scanf("%lf",&prices);
mooncakes[i].prices=prices;
mooncakes[i].rate=mooncakes[i].prices/(double)mooncakes[i].amounts;
}
sort(mooncakes.begin(),mooncakes.end(),Greater());
double maxProfit=0.0;
for(i=;i<N&&D>;++i)
{
if(D>=mooncakes[i].amounts)
{
maxProfit+=mooncakes[i].prices;
D-=mooncakes[i].amounts;
}
else
{
maxProfit+=mooncakes[i].rate*D;
D=;
}
}
printf("%.2f\n",maxProfit);
return ;
}

PAT 1070. Mooncake (25)的更多相关文章

  1. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

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

  2. PAT 1070 Mooncake[一般]

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

  3. 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

  4. PAT Advanced 1070 Mooncake (25) [贪⼼算法]

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

  5. PAT (Advanced Level) 1070. Mooncake (25)

    简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vecto ...

  6. PAT甲题题解-1070. Mooncake (25)-排序,大水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  7. 1070. Mooncake (25)

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

  8. PAT 1070 Mooncake

    题目意思能搞成这样我也是服了这个女人了 #include <cstdio> #include <cstdlib> #include <vector> #includ ...

  9. 1070 Mooncake (25 分)

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

随机推荐

  1. php版网易视频云api

    最近在做在线教育课程,使用网易云视频作为在线视频直播. 网易官方只有java示例,我们使用php,就自己写个api. 当然实现也是很简单的. 演示:http://www.deitui.com/inde ...

  2. 字符串匹配的python实现

    所有字符串匹配算法的核心问题是,当出现不匹配时,如何向后移动模式串 一.暴力匹配算法 如果要匹配一个字符串s 和一个模式串p,则从i=0开始依次匹配s[i:(i+len(p))],简单粗暴,代码如下: ...

  3. C#中的委托用法

    当一个函数有返回值的时候用,用Func委托方法. 例如: static int sum(int x) { return x+x; } Func<int> a = sum; 当一个函数没有返 ...

  4. 工作流软件如何成为未来web的支柱

    此文作者是 Kevin Lindquist,工作流平台Decisions的营销负责人,原文发表于VB上. Web 3.0 正在敲门,但是开门的人你永远都想不到:工作流软件. 传统上工作流软件是企业级的 ...

  5. 【转】mybatis在xml文件中处理大于号小于号的方法

    http://blog.csdn.net/zheng0518/article/details/10449549 第一种方法: 用了转义字符把>和<替换掉,然后就没有问题了. SELECT ...

  6. react + iscroll5

    react + iscroll5 经过几天的反复折腾,总算做出一个体验还不错的列表页了,主要支持了下拉刷新,上拉加载两个功能. 一开始直接采用了react-iscroll插件,它是基于iscroll插 ...

  7. 微软CSS面试全记录

    先是会有一轮简单的电话技术面试,聊的比较随意,什么都会问,跟职位相关的都有.然后会发一些材料说是要学习,是windows内存管理相关的东西. 完了就是一轮oral test,和技术没有任何关系,问问为 ...

  8. Array 原型扩展(快速排序,搅乱顺序)

    /// 快速快速排序算法Array.prototype.quickSort = function (left, right) { // left = left || 0; // right = rig ...

  9. 有关ARM大小端及网络字节序

    http://blog.sina.com.cn/s/blog_62b250b50101ntjs.html

  10. [转贴] C/C++中动态链接库的创建和调用

    DLL 有助于共享数据和资源.多个应用程序可同时访问内存中单个DLL 副本的内容.DLL 是一个包含可由多个程序同时使用的代码和数据的库.下面为你介绍C/C++中动态链接库的创建和调用. 动态连接库的 ...