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返回json数据函数实例

    本文实例讲述了php返回json数据函数的用法,分享给大家供大家参考.具体方法如下: json_encode()函数用法: ? 1 echo json_encode(array('a'=>'bb ...

  2. PHP学习笔记(2) - 对PHP的印象

    一.PHP是一种简单易学的面向过程的弱类型动态脚本语言,本为制作简单的个人网站而开发,现如今经过多个版本的衍变甚至加入了一些面向对象的特性.PHP试图通过发展打进企业级开发,同时也使得它自身也越来越复 ...

  3. appium的安装过程(图文界面)

    资料来源:http://www.cnblogs.com/fnng/p/4560298.html 1.准备安装材料

  4. insert遭遇阻塞

    insert的阻塞确实不常见,今天碰到了一个,看书又了解一个,整理下.1.多个会话同时向unique字段插入相同的值session1:首先建测试表test,并在字段id上创建一个主键索引(唯一键也可以 ...

  5. 大数据量查询优化——数据库设计、SQL语句、JAVA编码

    数据库设计方面: 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将 ...

  6. jQuery预加载插件

      插件描述:jQuery Fadeloader的插件可以让你轻松实现预加载到您的网站或部分使用级联渐显效果来显示特定的内容块(例如,头> MENU>内容>页脚) jQuery Fa ...

  7. 【SGU 390】Tickets (数位DP)

    Tickets   Description Conductor is quite a boring profession, as all you have to do is just to sell ...

  8. 【poj3070】矩阵乘法求斐波那契数列

    [题目描述] 我们知道斐波那契数列0 1 1 2 3 5 8 13…… 数列中的第i位为第i-1位和第i-2位的和(规定第0位为0,第一位为1). 求斐波那契数列中的第n位mod 10000的值. [ ...

  9. ANDROID_MARS学习笔记_S05_003_传感器采样率及属性

    1. 2. import android.app.Activity; import android.content.Context; import android.hardware.Sensor; i ...

  10. windows下安装php真正的多线程扩展pthreads教程

    扩展地址:http://docs.php.net/manual/zh/book.pthreads.php 注意事项php5.3或以上,且为线程安全版本.apache和php使用的编译器必须一致.通过p ...