当前位置:网站首页>L2-003 月饼 (25 分)(贪心)
L2-003 月饼 (25 分)(贪心)
2022-04-21 11:29:00 【.Ashy.】
描述:
月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。
注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有 3 种月饼,其库存量分别为 18、15、10 万吨,总售价分别为 75、72、45 亿元。如果市场的最大需求量只有 20 万吨,那么我们最大收益策略应该是卖出全部 15 万吨第 2 种月饼、以及 5 万吨第 3 种月饼,获得 72 + 45/2 = 94.5(亿元)。
输入格式
每个输入包含一个测试用例。每个测试用例先给出一个不超过 1000 的正整数 N 表示月饼的种类数、以及不超过 500(以万吨为单位)的正整数 D 表示市场最大需求量。随后一行给出 N 个正数表示每种月饼的库存量(以万吨为单位);最后一行给出 N 个正数表示每种月饼的总售价(以亿元为单位)。数字间以空格分隔。
输出格式
对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后 2 位。
思路:
贪心,按照月饼每万吨的单价由高到低排序,依次处理;
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e4+10;
struct node{
double key;//数量
double price;//价格
double ave; //单价
}a[N];
int n,sum;
double sums;//最大收益
bool cmp(node a,node b)
{
return a.ave>b.ave;
}
int main()
{
cin>>n>>sum;
for(int i=1;i<=n;i++)
{
scanf("%lf",&a[i].key);
}
for(int i=1;i<=n;i++)
{
scanf("%lf",&a[i].price);
a[i].ave=a[i].price/a[i].key;
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++)
{
if(sum>=a[i].key)
{
sums+=a[i].price;
sum-=a[i].key;
}
else
{
sums+=a[i].ave*sum;
break;
}
}
printf("%.2lf",sums);
}
版权声明
本文为[.Ashy.]所创,转载请带上原文链接,感谢
https://blog.csdn.net/woshilichunyang/article/details/124310743
边栏推荐
- Difference between ov code signature and ev code signature certificate
- Matlab --- select provinces and cities for application
- [binary number] symmetric binary tree
- GET 与 POST请求
- 2.精准营销实践阿里云odpscmd数据特征工程.
- 常见工具 nc Wireshark反弹shell
- El expression
- ES6新特性(8)之Decorator修饰器/二进制数组
- Three years of product manager, from 5K to 30K, I grew up like this (Part 1)
- 10000 yuan gift pool play "Lighthouse" prize essay attack
猜你喜欢

程序员如何确保软件没 Bug?

Leetcode1615. 最大网络秩(medium,图论基础)

Tami dog knowledge | what are the legal procedures for equity transfer?

微信小程序转uniapp

常见工具 nc Wireshark反弹shell

Dapr 远程调试之 Nocalhost

Interpretation of tamigou project | 100% equity transfer of Hainan outlets Tourism Development Co., Ltd

Reading material: Information Technology Yearbook

10000 yuan gift pool play "Lighthouse" prize essay attack

Solution to the display of the name of xftp file
随机推荐
Pgpool II 4.3 Chinese Manual - introductory tutorial
Convenience stores are crazy: convenience bee, Rosen and Yijie "fierce battle"
Filter
How to quickly build an app like Ding Dong shopping?
Filter
Calculate the n-bit sum of integers (C language)
When you open the app, "you need to use a new app to open this MS gamingoverlay link"
Leetcode1615. 最大网络秩(medium,图论基础)
实现将80端口请求转发到其他端口
InfoQ settled Express
Interpretation of tamigou project | 100% equity transfer of Hainan outlets Tourism Development Co., Ltd
Huawei cloud MySQL cloud database can easily help data to the cloud
JSON and related
redis面试问题
54000 stars all return to zero. Project Author: I regret it very much
Get and post requests
stm32i2c的解答
[二叉数]对称二叉树
Redis集群模式
L2-004 is this a binary search tree? (25 points)