PAT 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 region's 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 different 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
此题没有什么难度,就是一个贪心算法,或者说是一个分数背包问题,唯一需要注意的是题目中不能讲amounts的类型设为int,要设为double,不然有一个测试点过不了的,PAT就是一个坑啊。
#include <vector>
#include <algorithm>
#include <cstdio>
#include <functional>
using namespace std; const int NUM=; struct Mooncake
{
double amounts;
double prices;
double rate;
Mooncake()
{
amounts=0.0;
prices=0.0;
rate=0.0;
}
}; class Greater:public binary_function<Mooncake,Mooncake,bool>
{
public:
bool operator()(const Mooncake& lhs,const Mooncake& rhs) const
{
return lhs.rate>rhs.rate;
}
}; vector<Mooncake> mooncakes;
int main()
{
int N,D;
double amounts,prices;
Mooncake buf;
scanf("%d%d",&N,&D);
int i=;
for(;i<N;++i)
{
scanf("%lf",&amounts);
buf.amounts=amounts;
mooncakes.push_back(buf);
}
for(i=;i<N;++i)
{
scanf("%lf",&prices);
mooncakes[i].prices=prices;
mooncakes[i].rate=mooncakes[i].prices/(double)mooncakes[i].amounts;
}
sort(mooncakes.begin(),mooncakes.end(),Greater());
double maxProfit=0.0;
for(i=;i<N&&D>;++i)
{
if(D>=mooncakes[i].amounts)
{
maxProfit+=mooncakes[i].prices;
D-=mooncakes[i].amounts;
}
else
{
maxProfit+=mooncakes[i].rate*D;
D=;
}
}
printf("%.2f\n",maxProfit);
return ;
}
PAT 1070. Mooncake (25)的更多相关文章
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 1070 Mooncake[一般]
1070 Mooncake (25)(25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Aut ...
- 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)
题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...
- PAT Advanced 1070 Mooncake (25) [贪⼼算法]
题目 Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many typ ...
- PAT (Advanced Level) 1070. Mooncake (25)
简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vecto ...
- PAT甲题题解-1070. Mooncake (25)-排序,大水题
#include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...
- 1070. Mooncake (25)
题目如下: Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many ...
- PAT 1070 Mooncake
题目意思能搞成这样我也是服了这个女人了 #include <cstdio> #include <cstdlib> #include <vector> #includ ...
- 1070 Mooncake (25 分)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn ...
随机推荐
- Winform与WPF对话框(MessageBox, Dialog)之比较
Winform:使用System.Windows.Forms命名空间中相应控件; WPF则调用Microsoft.Win32. MessageBox: // WinForm private void ...
- JavaMail 发送邮件
JavaMail邮件发送 引用maven jar包 <dependency> <groupId>javax.mail</groupId> <artifactI ...
- 寡人写的第一个HTML5页面
好吧,其实是抄来的 <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"/> &l ...
- C#在声明对象时对其赋值的一种方式
今天学会一种更方便的赋值方式,如下, 同时存档一个通过 打开对话框 获取地址的方式. private string GetSaveAsPathXls(string defaultFileName) { ...
- 总结iframe高度自适应,自适应子页面高度
在网上找了很多iframe的高度自适应,发现很多兼容性都不是很好,于是自己总结了一下. 页面html节点上要有 <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- 重燃你的PHP安全分析之火
关于脚本安全这个话题好像永远没完没了,如果你经常到国外的各种各样的bugtraq上,你会发现有一半以上都和脚本相关,诸如SQL injection,XSS,Path Disclosure,Remote ...
- hdu 4752
计算几何+数值计算的题目: 要用到辛普森积分,没有学过~~~ 参考学习了acm_Naruto大神 的代码! 代码: #include<cstdio> #include<cmath&g ...
- hdu 4442
一道超级easy的贪心 一眼看出了他的本质: 代码: #define mod 31536000 #include<cstdio> #include<algorithm> #in ...
- WordPress 全方位优化指南(上)
作为一个全面的 WordPress 性能优化教程,本文旨在帮助读者排查 WordPress 网站的性能问题,同时也提供网站前端优化加速的建议. 如果你曾经遇到过 WordPress 管理界面加载缓慢. ...
- struts2文件下载出现Can not find a java.io.InputStream with the name的错误
今天在用struts2就行文件下载时出现如下错误: Servlet.service() for servlet default threw exception java.lang.IllegalArg ...