1020. 月饼 (25)
来源:互联网 发布:数据服务 编辑:程序博客网 时间:2024/05/09 13:02
1020. 月饼 (25)
时间限制
100 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼。现给定所有种类月饼的库存量、总售价、以及市场的最大需求量,请你计算可以获得的最大收益是多少。
注意:销售时允许取出一部分库存。样例给出的情形是这样的:假如我们有3种月饼,其库存量分别为18、15、10万吨,总售价分别为75、72、45亿元。如果市场的最大需求量只有20万吨,那么我们最大收益策略应该是卖出全部15万吨第2种月饼、以及5万吨第3种月饼,获得 72 + 45/2 = 94.5(亿元)。
输入格式:
每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N表示月饼的种类数、以及不超过500(以万吨为单位)的正整数D表示市场最大需求量。随后一行给出N个正数表示每种月饼的库存量(以万吨为单位);最后一行给出N个正数表示每种月饼的总售价(以亿元为单位)。数字间以空格分隔。
输出格式:
对每组测试用例,在一行中输出最大收益,以亿元为单位并精确到小数点后2位。
输入样例:3 2018 15 1075 72 45输出样例:
94.50
不要忽略精度问题
#include<iostream>#include<algorithm>#include<string>#include<iomanip>using namespace std;struct pct{ double num; double pro; double pri;};bool cmp(pct a, pct b){ return a.pri>b.pri;}int main(){ ios::sync_with_stdio(false); int n, m; cin>>n>>m; pct *p = new pct[n]; for(int i=0;i<n;++i) { cin>>p[i].num; } for(int i=0;i<n;++i) { cin>>p[i].pro; p[i].pri=p[i].pro/p[i].num; } sort(p, p+n, cmp); double sum = 0; int ss = p[0].num; int dex = 0; int temp = 0; double tp = 0; for(int i=0; i<n; ++i) { temp+=p[i].num; tp += p[i].pro; } if(ss > m) cout<<fixed<<setprecision(2)<<m*p[0].pri<<endl; else if(temp <= m) cout<<fixed<<setprecision(2)<<tp<<endl; else { do{ sum += p[dex].pro; ss += p[++dex].num; int t = m - ss; if(t < 0) { int x = m - ss + p[dex].num; sum += x * p[dex].pri; break; } else if(t == 0) { sum +=p[dex].pro; break; } }while(1); cout<<fixed<<setprecision(2)<<sum<<endl; } return 0;}
阅读全文
0 0
- PAT 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 1020. 月饼 (25)
- 翻转单词顺序
- 初识多线程 (其二)
- 类加载器
- 二阶构造
- MySQL和Oracle
- 1020. 月饼 (25)
- 单例模式+Java
- CSDN我又回来了
- C语言梳理指针的混淆点
- LinuxKernelDevelopment_读书笔记
- grep正则表达式(二)
- 无法访问虚拟机no route to host 解决 .
- c++4
- 《Core Java》读书笔记——第4章