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个正数表示某种月饼库存全 ...
随机推荐
- border-radius 在 浏览器开发者工具移动端里是有效的,在真机是无效的。
border-radius 在 浏览器开发者工具移动端里是有效的,在真机是无效的,怎么解决? 答案是 border-radius:20px !important 加上!important 就好了.
- UVA 10564 计数DP
也是经典的计数DP题,想练练手,故意不写记忆化搜索,改成递推,还是成功了嘞...不过很遗憾一开始WA了,原来是因为判断结束条件写个 n或s为0,应该要一起为0的,搞的我以为自己递推写挫了,又改了一下, ...
- 201912-1 报数 Java
思路: String.valueOf(int i) : 将 int 变量 i 转换成字符串 String.contains()用于判断字符串是否包含子字符串 import java.util.Scan ...
- (排序)P1068 分数线划定
题解: 需要注意的是,快排完之后并不是按照编号从小到大的顺序输出 #include<iostream>using namespace std;int r=0;void swap(int & ...
- FTP故障排除
1,ping 检查 IP是否通 禁PING可以使用TCPING 2,服务器端被动模式设置,可设置固定端口号,保证防火墙上该端口畅通 浏览器默认是主动模式 3,使用FLASHFXP软件可以监测到数据端口 ...
- linux下软件包的安装方式
参考:https://blog.51cto.com/13589255/2071277
- Java算法练习——回文数
题目链接 题目描述 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1 输入: 121 输出: true 示例 2 输入: -121 输出: false ...
- 关于SI4432数据手册的简单讲解
SI4432是高度集成度单芯片无线ISM收发器件.EZRadioPRO系列包括了发射机.接收机和射频收发器,让设计工程师可以有选择的设计利用里面的无线部分. SI4432提供了先进的无线功能,包括连续 ...
- 4 ~ express ~ 划分模块开发
一,根据功能进行模块划分 1,前台模块 2,后台管理模块 3,API模块 二,使用 app.use() 进行模块划分 1,app.use('/',require('./router/main')) 1 ...
- Resource interpreted as Stylesheet but transferred with MIME || DevTools failed to parse SourceMap:
最近在学SpringBoot,在整合Thymeleaf的时候,配置拦截器.教学上讲SpringBoot已经做好了静态资源映射,所以不需要特地去做排除拦截 以下代码就是我在做登录拦截的时候配置的拦截. ...