题目:https://pintia.cn/problem-sets/994805260223102976/problems/994805301562163200
思路
销售时允许取出一部分库存。
样例给出的情形是这样的:
假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨
总售价分别为 75、72、45 亿元。
如果市场的最大需求量只有 20 万吨,
那么我们最大收益策略应该是卖出全部 15 万吨第 2 种月饼、以及 5 万吨第 3 种月饼,获得 72 + 45/2 = 94.5(亿元)
所以可以把月饼按照单价排名
先把贵的售空,再一级级往下
至于控制格式,只需要加一个<cstdio>的头文件,按C语言格式输出即可
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct Mooncake{
// 库存量 总价 单价
double num,sum,pre;
}mc[1000];
bool cmp(Mooncake a, Mooncake b){
return a.pre>b.pre;// 把单价高的扔在前面
}
int main() {
// 种类(<1000) 需求(<500)
int type,demand;
cin>>type>> demand;
for(int i=0; i<type; i++){
cin>>mc[i].num;
}
for(int i=0; i<type; i++){
cin>>mc[i].sum;
}
for(int i=0; i<type; i++){
mc[i].pre= mc[i].sum/mc[i].num;
}
sort(mc, mc+type ,cmp);
double money=0;
for(int i=0; i<type; i++){
// 需求量少于当前库存
if(demand <= mc[i].num){
money+= mc[i].pre*demand;
break;
}
else{
money += mc[i].sum;
demand-= mc[i].num;
}
}
printf("%0.2f\n", money);
return 0;
}
本文通过一个具体的例子介绍了一种最优销售策略。假设存在多种月饼,每种有不同的库存量和总售价,当市场需求有限时,如何最大化收益。算法首先计算每种月饼的单价,然后将月饼按单价从高到低排序,并尽可能先出售单价较高的月饼。
1374

被折叠的 条评论
为什么被折叠?



