Mooncake (排序+贪心)
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
坑点:1、这种题目,最好把所以进行运算的变量都设为double。2、最大总量可能为0。
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
struct moom
{
double pr;
double cc;
};
bool cmp(moom a,moom b)
{
return a.pr/a.cc>b.pr/b.cc;
}
moom mm[];
int main()
{
int n,i;
double d;
while(cin>>n)
{
cin>>d;
for(i=;i<n;i++)
{
cin>>mm[i].cc;
}
for(i=;i<n;i++)
{
cin>>mm[i].pr;
}
sort(mm,mm+n,cmp);
i=;double sum=;
while(true)
{
if(mm[i].cc==) break;
if(d<=mm[i].cc)
{
sum=sum+d/mm[i].cc*mm[i].pr;
break;
}
else
{
d=d-mm[i].cc;
sum=sum+mm[i].pr;
i++;
}
}
cout<<fixed<<setprecision()<<sum<<endl;
}
return ;
}
Mooncake (排序+贪心)的更多相关文章
- BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心
BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心 Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜 ...
- CodeForces 1294B Collecting Packages(排序+贪心)
http://codeforces.com/contest/1294/problem/B 大致题意: 一张图上有n个包裹,给出他们的坐标,一个机器人从(0,0)出发,只能向右(R)或向上(U),问能否 ...
- POJ3687 Labeling Balls(拓扑排序\贪心+Floyd)
题目是要给n个重量1到n的球编号,有一些约束条件:编号A的球重量要小于编号B的重量,最后就是要输出字典序最小的从1到n各个编号的球的重量. 正向拓扑排序,取最小编号给最小编号是不行的,不举出个例子真的 ...
- poj1456 结构体排序+贪心
题意:给出很多商品,每个商品有价值和出售期限,只能在期限内出售才能获取利润,每一个单位时间只能出售一种商品,问最多能获得多少利润. 只需要按照优先价值大的,其次时间长的排序所有物品,然后贪心选择,从它 ...
- poj-2376 Cleaning Shifts (排序+贪心)
http://poj.org/problem?id=2376 john有n头牛做打扫工作,他想在t时间内每个时间都至少有一头牛在做打扫工作,第一头牛在1,最后一头牛在t时间,每一头牛工作都有一个开始时 ...
- vijos 1605 双栈排序 - 贪心 - 二分图
题目传送门 传送门I 传送门II 题目大意 双栈排序,问最小字典序操作序列. 不能发现两个数$a_{j}, a_{k}\ \ (j < k)$不能放在同一个栈的充分必要条件时存在一个$i$使得$ ...
- 2019.01.20 bzoj5158 Alice&Bob(拓扑排序+贪心)
传送门 短代码简单题. 题意简述:对于一个序列XXX,定义其两个伴随序列a,ba,ba,b,aia_iai表示以第iii个数结尾的最长上升子序列长度,bib_ibi表示以第iii个数开头的最长下降 ...
- HDU 4857 逃生(反向建边的拓扑排序+贪心思想)
逃生 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...
- 洛谷P2127 序列排序 [贪心]
题目传送门 题目描述 小C有一个N个数的整数序列,这个序列的中的数两两不同.小C每次可以交换序列中的任意两个数,代价为这两个数之和.小C希望将整个序列升序排序,问小C需要的最小代价是多少? 输入输出格 ...
随机推荐
- Java设计模式14:常用设计模式之组合模式(结构型模式)
http://blog.csdn.net/jason0539/article/details/22642281
- oracle--number
1. oracle的number类型是oracle的内置类型之一,是oracle的最基础数值数据类型.在9iR2及其以前的版本中只支持一种适合存储数值数据的固有数据类型,在10g以后,才出现了两种新的 ...
- [设计模式]<<设计模式之禅>>关于迪米特法则
迪米特法则(Law of Demeter,LoD)也称为最少知识原则(Least KnowledgePrinciple,LKP),虽然名字不同,但描述的是同一个规则:一个对象应该对其他对象有最少的了解 ...
- nodejs自己在项目中使用的一个工具库utils.js文件
文件内容如下: /** * utils.js */ var crypto = require('crypto'); var url = require('url'); var querystring ...
- [转]Ubuntu中无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用)
sudo apt-get install git E: 无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用) E: 无法锁定管理目录(/var/lib/dpkg/ ...
- IOS Delegate & protocal
总结一下: delegate是一个方式,程序组成单元之间分工的一种协调思想 protocal 这个东西不能单独说,要与它相关的两个主要东西一起说,一个是 委托者 通常是VIEW, 一个是被委托者 通常 ...
- 【转载】Android设计中的.9.png
转载自:腾讯ISUX (http://isux.tencent.com/android-ui-9-png.html) 在Android的设计过程中,为了适配不同的手机分辨率,图片大多需要拉伸或者压 ...
- Javascript之登陆验证
匹配中文字符的正则表达式: [\u4e00-\u9fa5] 匹配双字节字符(包括汉字在内):[^\x00-\xff] 匹配空行的正则表达式:\n[\s| ]*\r 匹配网址URL的正则表达式:http ...
- Memcached学习(二)
4.协议简介 Memcached服务与各客户端间通过Tcp链接通讯(也可通过Udp链接). 各客户端间与服务间不需要发送特别的命令关闭链接,只要在不需要的时候直接关闭链接即可.建议客户端与服务间保持长 ...
- stl中的map数据类型
1.1 STL map 1.1.1 背景 关联容器使用键(key)来存储访问读取元素,而顺序容器则通过元素在容器中的位置存储和访问元素. 常见的顺序容器有:vector.list.deque.stac ...