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. cli下的php(并传递参数)

    传递参数有两种方式: 第一种使用文件操作,STDOUT作为标准输出,STDIN作为标准输入 使用fwrite($file,$string)作输出,使用fgets($file)作输入.这种应该算是继承自 ...

  2. centos下redis安装

    下载redis http://www.redis.cn/download.html 下载php的redis扩展 https://github.com/phpredis/phpredis#install ...

  3. lucene开发序之luke神器

    lucene是一款很优秀的全文检索的开源库,目前最新的版本是lucene4.4,关于lucene的历史背景以及发展状况,在这里笔者就不多介绍了,如果你真心想学习lucene,想必在这之前你已经对此作过 ...

  4. hdu 5612 Baby Ming and Matrix games

    Baby Ming and Matrix games 题意: 给一个矩形,两个0~9的数字之间隔一个数学运算符(‘+’,’-‘,’*’,’/’),其中’/’表示分数除,再给一个目标的值,问是否存在从一 ...

  5. Discuz帖子列表页无法ajax加载下一页问题

    上周末抽空重构了一下JX3PVE的PVE栏目,只上线了宏库栏目,结果出了一堆Bug.奈何公司这段时间都在搞完美假期这专题,太忙也没去处理.这不是周末拿时间来看一下. 发现其中有一个是点“下一页”aja ...

  6. 第 1 章 策略模式【Strategy Pattern】

    第 1 章 策略模式[Strategy Pattern] 以下内容出自: 24种设计模式介绍与6大设计原则.pdf 刘备要到江东娶老婆了,走之前诸葛亮给赵云(伴郎)三个锦囊妙计,说是按天机拆开解决棘手 ...

  7. POJ 1778 All Discs Considered(拓扑排序)

    点我看题目 题意 :其实题意我也说不清楚,因为比赛的时候我盯着看了1个小时也没看懂....就是两个磁盘,第一个有n1的安装包,编号为1~n1,第二个有n2个安装包,编号为n1~n2.给你d对关系,(x ...

  8. NODE.JS玩玩

    按一个网页的来,最好最后能到EXPRESS.JS. http://www.nodebeginner.org/index-zh-cn.html 这样就能对比DJANGO,看看两者的WEB框架,加深认识. ...

  9. 【Hihocoder 1167】 高等理论计算机科学 (树链的交,线段树或树状数组维护区间和)

    [题意] 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 少女幽香这几天正在学习高等理论计算机科学,然而她什么也没有学会,非常痛苦.所以她出去晃了一晃,做起了一些没什么意 ...

  10. android 对象传输及parcel机制

    在开发中不少要用到Activity直接传输对象,下面我们来看看,其实跟java里面差不多   自定义对象的传递:通过intent传递自定义对象的方法有两个  第一是实现Serialization接口: ...