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个正数表示某种月饼库存全 ...
随机推荐
- [题解] LuoguP4609 [FJOI2016]建筑师
传送门 首先对于高度为\(n\)的建筑,他的左边有\(A-1\)个建筑能被看到,右边有\(B-1\)个建筑能被看到,这两者类似,所以先来看左边. 一个建筑将会遮挡住它后面的高度比它矮的建筑,直到一个高 ...
- linux项目,项目报错,排查
今天在遇到以前部署的项目突然没有数据了,然后就去服务器看了一下,打印日志发现报错了,现在我是一脸懵逼,因为不知道怎么排查 然后同事告诉说先看报错的原因,然后再去找认识的类,我打码的都是一些认识的 然后 ...
- hdu 1950 Bridging signals 求最长子序列 ( 二分模板 )
Bridging signals Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 十八、CI框架之数据库操作update用法
一.代码如图: 二.访问一下 三.我们来查看数据库,已经被修改了 不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信二维码打赏任意金额给作者(微信号:382477247)哦,谢谢.
- 用tensorflow求手写数字的识别准确率 (简单版)
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data #载入数据集 mnist = in ...
- (5)opencv的基础操作和矩阵的掩模操作
不懂的,可以简单,看看这个网址:https://blog.csdn.net/xiongwen_li/article/details/78503491 图片放到了桌面,所以,图片的路径就是桌面了,剩余的 ...
- Maven - 工作原理
章节 Maven – 简介 Maven – 工作原理 Maven – Repository(存储库) Maven – pom.xml 文件 Maven – 依赖管理 Maven – 构建生命周期.阶段 ...
- POJ 1062:昂贵的聘礼
昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 40715 Accepted: 11839 Descripti ...
- kube-apiserver常用配置项
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0" ...
- Java 类提供了自定义的构造方法,那么类的默认构造不会被调用
以下代码无法通过编译: public class Test1 { public static void main(String[] args) { //int a=6; Foo obj=new Foo ...