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 ...
随机推荐
- sass中 混合宏 VS 继承 VS 占位符 各自的使用时机和特点
初学者都常常纠结于这个问题“什么时候用混合宏,什么时候用继承,什么时候使用占位符?”其实他们各有各的优点与缺点,先来看看他们使用效果: a) Sass 中的混合宏使用 举例代码见 2-24 行 编译出 ...
- CSS2书写顺序
1.位置属性(display, float, position, top, right, z-index, visibility,clear,list-style等) 2.自身属性(width, he ...
- C++ 11 笔记 (三) : auto
我真的不是标题党... 虽然大一上学期学C语言基础时就学了auto关键字了,而且还是跟static和register两个关键字打包学的,但是.. 猜的没错,C++11这货又给auto加新功能了,在 C ...
- C# Post Json数据
public string Post(string Url, string jsonParas) { string strURL = Url; //创建一个HTTP请求 ...
- ACdream训练赛系列のJava专场
/* * this code is made by mhy12345 * Problem: 1669 * Verdict: Accepted * Submission Date: 2015-04-21 ...
- bzoj 2746: [HEOI2012]旅行问题 AC自动机fail树
2746: [HEOI2012]旅行问题 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 489 Solved: 174[Submit][Status ...
- Word删除复制后产生空行
如上图,用网上说的^p^p替换为^p无效,我全选,然后在样式里选择正文才搞定.实践出真知!!!!!!!!!网络上附和的多
- Metasploit介绍
Metasploit是一款开源的安全漏洞检测工具,可以帮助安全和IT专业人士识别安全性问题,验证漏洞的缓解措施,并管理专家驱动的安全性进行评估, 提供真正的安全风险情报.这些功能包括智能开发,密码审计 ...
- 转:Nginx 日志文件切割
http://www.cnblogs.com/benio/archive/2010/10/13/1849935.html 偶然发现access.log有21G大,所以将其切割. Nginx 是一个非常 ...
- PHP 判断是表单否有这个参数,如果没有则设置默认值
<?php @$name = $_GET["name"]; if(isset($name)) { echo "name = " .$name; } els ...