题目如下:

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

题目要求按照利益最大化的原则出售月饼来满足市场需求,属于基本贪心问题。

应该按照公斤单价的降序排序,然后从单价最高的开始处理,直到处理完所有种类的月饼或者市场需求已经被满足。

需要注意的是,存货应该也用double存储,否则会有个case报错,可能是精度问题。

代码如下:

#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm> using namespace std; struct Cake{
double inventory;
double price;
double average; bool operator < (const Cake c) const{
return average > c.average;
} }; int main()
{
int N,D;
cin >> N >> D;
vector<Cake> cakes(N);
for(int i = 0; i < N; i++){
scanf("%lf",&cakes[i].inventory);
}
for(int i = 0; i < N; i++){
scanf("%lf",&cakes[i].price);
cakes[i].average = cakes[i].price / cakes[i].inventory;
}
sort(cakes.begin(),cakes.end());
int cur = 0;
double sum = 0;
while(D && cur < N){
if(cakes[cur].inventory >= D){
sum += cakes[cur].average * D;
break;
}else{
sum += cakes[cur].price;
D -= cakes[cur].inventory;
cur++;
}
}
printf("%0.2lf\n",sum);
return 0;
}

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 (25)

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

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

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

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

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

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

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

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

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

  7. PAT 1070 Mooncake[一般]

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

  8. 1070 Mooncake (25 分)

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

  9. 1070 Mooncake

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

随机推荐

  1. Bubble Cup X - Finals [Online Mirror]

    来自FallDream的博客,未经允许,请勿转载,谢谢. 组了个菜鸡队打cf上的ACM比赛 比较快做完了8题但是菜的抠脚罚时巨多,所以最后被顶到了19名(居然没出首页) 自己的号自从上次疯狂掉分就没动 ...

  2. 例10-5 uva12716

    题意:gcd(a,b) = a^b,( 1≤ a , b ≤ n) 思路: ① a^b = c, 所以 a^c = b,而且c是a的约数,枚举a,c,再gcd判断 ② 打表可知 a-b = c,而且a ...

  3. bzoj4767两双手 容斥+组合

    4767: 两双手 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 684  Solved: 208[Submit][Status][Discuss] ...

  4. 数据结构之B树、B+树(一)

    B-树 什么是B-树? B树是一种查找树,我们知道,这一类树(比如二叉搜索树,红黑树等等)最初生成的目的都是为了解决某种系统中,查找效率低的问题.B树也是如此,它最初启发于二叉搜索树,二叉搜索树的特点 ...

  5. Eclipse插件安装4种方法

    第一种:直接复制法 假设Eclipse的安装目录在C:\eclipse,解压下载的eclipse 插件或者安装eclipse 插件到指定目录AA(如:c:\AA)文件夹,打开AA 文件夹,在AA文件夹 ...

  6. 解决IE6下select显示在弹出框上问题

    利用定位position:absolute;z-index:1;和iframe[z-index:-1]来解决此问题,最好根据需要加上: border='0' frameborder='0' scrol ...

  7. CTR预估算法

    GBRT(Gradient Boost Regression Tree)渐进梯度回归树,XGBoost是GBRT的一个工程实现 LR(Logistics Regression )逻辑回归 Spark ...

  8. linux 3.10 缺页异常(TLB_invalid)通用处理框架

  9. [转载]致创业者:APP已死 服务永生

    前几日,有位创业者和我讲他在带领团队做一个将爱踢球的人集中在一起的App,我告诉他你的创业方向错了.原因在于你的目的是要为爱踢球的人提供服务,而你现在却在竭尽全力的做App,你应该做的是设计你为爱踢球 ...

  10. Redis从入门到精通:中级篇

    原文链接:http://www.cnblogs.com/xrq730/p/8944539.html,转载请注明出处,谢谢 本文目录 上一篇文章以认识Redis为主,写了Redis系列的第一篇,现在开启 ...