题目如下:

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. Python中的文件路径的分隔符

    主要是需要考虑分隔符的问题: 在Windows系统下的分隔符是:\ (反斜杠). 在Linux系统下的分隔符是:/(斜杠). 当在字符中出现\时,大家就要考虑到转义字符了. 转义字符的概念,参考维基百 ...

  2. PSR-4 自动加载器

    div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,cod ...

  3. js中json字符串与json对象的相互转换

    web前端开发过程中,数据传输json是以字符串的形式传递,而js操作的是JSON对象. 一.JSON字符串转换为JSON对象 var obj = JSON.parse(str[, reviver]) ...

  4. 初识Redis系列之一:简单介绍

    一:Redis是什么? Redis全称:REmote DIctionary Server(Redis) .Redis是一个由Salvatore Sanfilippo写的key-value存储系统,AN ...

  5. 关于在同一个DIV下的Hover效果问题

    例子: (function bindColumnRowHoverEvent(){ $('.ticket_list_body .work_product').live('mouseenter', fun ...

  6. [JCIP笔记](五)JDK并发包

    这一节来讲一讲java.util.concurrent这个包里的一些重要的线程安全有关类. synchronized容器 synchronized容器就是把自己的内部状态封装起来,通过把每一个publ ...

  7. Hibernate异常之关键字错误

    三月 08, 2018 7:50:25 下午 org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException ...

  8. Node.js 字符串解码器

    稳定性: 3 - 稳定 通过 require('string_decoder') ,可以使用这个模块.字符串解码器(StringDecoder)将缓存(buffer)解码为字符串.这是 buffer. ...

  9. 深度解读GoogleNet之Inception V1

    GoogleNet设计的目的 GoogleNet设计的初衷是为了提高在网络里面的计算资源的利用率. Motivation 网络越大,意味着网络的参数较多,尤其当数据集很小的时候,网络更容易发生过拟合. ...

  10. 重载equals方法时要遵守的通用约定--自反性,对称性,传递性,一致性,非空性

    本文涉及到的概念 1.为什么重载equals方法时,要遵守通用约定 2.重载equals方法时,要遵守哪些通用约定   为什么重载equals方法时,要遵守通用约定 Object类的非final方法都 ...