题目

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 regions 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 diferent 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

题目分析

已知不同种类月饼的库存,总价格,市场需求量,求最大利润

解题思路

  1. 贪心思想:依次获取单位价格最贵的月饼
  2. 结构体moon,存储月饼的库存,总价格,单位价格(输入总价格时预处理,减少一次遍历)
  3. 按照单位价格降序排序,依次获取,直到达到市场需求量

Code

Code 01

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct moon {
double w; // total weight (in thousand tons)
double p; // total price (in billion yuan)
double pp; // price per thousand ton
};
bool cmp(moon &m1,moon &m2) {
return m1.pp>m2.pp;
}
int main(int argc, char * argv[]) {
// 1 接收数据
int N,D;
scanf("%d %d",&N,&D);
vector<moon> vms(N); //存放月饼数据
for(int i=0; i<N; i++) scanf("%lf", &vms[i].w);
for(int i=0; i<N; i++) {
scanf("%lf", &vms[i].p);
vms[i].pp = vms[i].p/vms[i].w; //计算每重量单位的价格
}
// 2 按照单位价格高-低排序
sort(vms.begin(),vms.end(),cmp);
// 3 求最大利润
double tp = 0.0;
for(int i=0; i<N&&D>0; i++) {
if(vms[i].w<D) {//如果当前月饼种类的重量小于需求剩余数,全部销售
D=D-vms[i].w;
tp+=vms[i].p;
} else { //如果当前月饼种类的重量大于需求剩余数,按照单位价计算剩余需求重量
tp+=D*vms[i].pp;
D=0;
}
}
printf("%.2f", tp);
}

PAT Advanced 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 Advanced 1003 Emergency (25) [Dijkstra算法]

    题目 As an emergency rescue team leader of a city, you are given a special map of your country. The ma ...

  3. PAT Basic 1020 ⽉饼 (25) [贪⼼算法]

    题目 ⽉饼是中国⼈在中秋佳节时吃的⼀种传统⻝品,不同地区有许多不同⻛味的⽉饼.现给定所有种类⽉饼的库存量.总售价.以及市场的最⼤需求量,请你计算可以获得的最⼤收益是多少. 注意:销售时允许取出⼀部分库 ...

  4. PAT Advanced 1067 Sort with Swap(0,*) (25) [贪⼼算法]

    题目 Given any permutation of the numbers {0, 1, 2,-, N-1}, it is easy to sort them in increasing orde ...

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

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

  6. PAT Advanced 1037 Magic Coupon (25) [贪⼼算法]

    题目 The magic shop in Mars is ofering some magic coupons. Each coupon has an integer N printed on it, ...

  7. PAT Advanced 1033 To Fill or Not to Fill (25) [贪⼼算法]

    题目 With highways available, driving a car from Hangzhou to any other city is easy. But since the tan ...

  8. PAT 1070. Mooncake (25)

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

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

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

随机推荐

  1. 微服务中一个项目install打包总是失败

    在微服务的一个项目中install打包时总是报错如下: [INFO] Scanning for projects... [INFO] [INFO] -------------------------- ...

  2. 【LeetCode】分发糖果

    [问题]老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分. 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个糖果.相邻的孩子中 ...

  3. poj 3693 Maximum repetition substring

    呵呵呵呵呵呵呵呵呵呵,sb(神犇)题看了一天,还是不懂 题目要求的是最多重复的,那么就来找重复的,可以先枚举一个重复的单元(比如ababab,就枚举ab)的长度, 然后再原串中,会有ch[0],ch[ ...

  4. 集合框架的详解,List(ArrayList,LinkedList,Vector),Set(HashSet,TreeSet)-(14)

    集合详解: /* Collection |--List:元素是有序的,元素可以重复.因为该集合体系有索引. |--ArrayList:底层的数据结构使用的是数组结构.特点:查询速度很快.但是增删稍慢. ...

  5. 2.10 Jetpack LiveData部分

    LiveData是一个可观察的数据持有者类,但和其他的可观察对象不同,它与生命周期相关联,比如Activity的生命周期.LiveData能确保仅在Activity处于活动状态下才会更新.也就是说当观 ...

  6. 爬虫(十八):Scrapy框架(五) Scrapy通用爬虫

    1. Scrapy通用爬虫 通过Scrapy,我们可以轻松地完成一个站点爬虫的编写.但如果抓取的站点量非常大,比如爬取各大媒体的新闻信息,多个Spider则可能包含很多重复代码. 如果我们将各个站点的 ...

  7. 优秀的linux学习网站

    从网络上拷贝别人归纳的列表. Linux优秀网站列表 国内 http://www.chinaunix.net/ 国内最火爆的unix/linux论坛 http://www.linuxforum.net ...

  8. MySQL-半同步复制原理实践

    参考文档: http://mysql.taobao.org/monthly/2017/04/01/ 阿里内核月报半同步复制的数据一致性 https://www.cnblogs.com/ivictor/ ...

  9. Android进阶——Android视图工作机制之measure、layout、draw

    自定义View一直是初学者们最头疼的事情,因为他们并没有了解到真正的实现原理就开始试着做自定义View,碰到很多看不懂的代码只能选择回避,做多了会觉得很没自信.其实只要了解了View的工作机制后,会发 ...

  10. python 发送邮件,并且带附件

    #!/usr/bin/pythonfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultiparti ...