PAT Advanced 1070 Mooncake (25) [贪⼼算法]
题目
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
题目分析
已知不同种类月饼的库存,总价格,市场需求量,求最大利润
解题思路
- 贪心思想:依次获取单位价格最贵的月饼
- 结构体moon,存储月饼的库存,总价格,单位价格(输入总价格时预处理,减少一次遍历)
- 按照单位价格降序排序,依次获取,直到达到市场需求量
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) [贪⼼算法]的更多相关文章
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- 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 ...
- PAT Basic 1020 ⽉饼 (25) [贪⼼算法]
题目 ⽉饼是中国⼈在中秋佳节时吃的⼀种传统⻝品,不同地区有许多不同⻛味的⽉饼.现给定所有种类⽉饼的库存量.总售价.以及市场的最⼤需求量,请你计算可以获得的最⼤收益是多少. 注意:销售时允许取出⼀部分库 ...
- 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 ...
- PAT (Advanced Level) 1070. Mooncake (25)
简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vecto ...
- 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, ...
- 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 ...
- PAT 1070. Mooncake (25)
Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...
- 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)
题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...
随机推荐
- ajax异步提交 有时会出现无bug的数据处理异常-----debug没有问题,正常运行却数据处理不正确,极少机会会出现正常的处理结果
ajax 被使用时,常默认的就使用了异步处理. 当遇到后面的代码对同样的数据进行处理 或 要依赖前面ajax处理的结果时,就会导致数据处理结果不正确,未达到预期值. 且,debug时却能正常完成功能 ...
- xml学习-语法规则
XML 指可扩展标记语言(eXtensible Markup Language).XML 被设计用来传输和存储数据. XML 语法规则 XML 文档必须有根元素 XML 必须包含根元素,它是所有其他元 ...
- Java算法练习——最长回文子串
题目链接 题目描述 给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1 输入: "babad" 输出: "bab" ...
- WindowsForm ComboBoxList 下拉框带复选框 可以动态添加
先来张效果图: 1.这里需要对控件进行重写,详细内容如下,对此不感兴趣的可以直接跳过这步,下载本人生成的dll,直接看第二小结,下载链接https://pan.baidu.com/s/1gfzrK5t ...
- 快速幂(51Nod1046 A^B Mod C)
快速幂也是比较常用的,原理在下面用代码解释,我们先看题. 51Nod1046 A^B Mod C 给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. In ...
- 吴裕雄--天生自然 PHP开发学习:PHP编程基础知识
<?php $x=5; $y=6; $z=$x+$y; echo $z; ?> <?php $txt="Hello world!"; $x=5; $y=10.5; ...
- Palette 的使用
Palette有什么用? Palette主要功能就是可以从图片中提取各种与颜色有关的元素.通过使用 Palette ,我们可以很轻松的实现界面风格的统一. Palette的使用很简单,首先你可以从gi ...
- 深入理解 Java —— GC 机制
1. 基础知识 1.1 什么是垃圾回收? 程序的运行必然需要申请内存资源,无效的对象资源如果不及时处理就会一直占有内存资源,最终将导致内存溢出,所以对内存资源的管理非常重要. 垃圾回收就是对这些无效资 ...
- 浅入深出Java输入输出流主线知识梳理
Java把不同类型的输入.输出,这些输入输出有些是在屏幕上.有些是在电脑文件上, 都抽象为流(Stream) 按流的方向,分为输入流与输出流,注意这里的输出输出是相对于程序而言的,如:如对于一个J ...
- composer命令卡慢,使用国内源
执行composer install.update 和require的时候,遇到卡住不动的情况,可以切换到国内阿里云的源 composer config -g repo.packagist compo ...