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需要的最小代价是多少? 输入输出格 ... 
随机推荐
- python(1) - 第一个程序 Hello World!
			进入python3的解释器环境. 我们让解释器输出 “Hello World!” 解释器成功的输出了Hello world! 程序就这样完成了. 当然上面的程序我们是在解释器中完成的. 我们可以通过 ... 
- Android 自学之画廊视图(Gallery)功能和用法
			Gallery与之前讲的Spinner有共同的父类:AbsSpinner,表明Gallery和Spinner都是一个列表框.他们之间的区别在于Spinner显示的是一个垂直的列表框,而Gallery显 ... 
- 如何添加PPA
			什么是PPA? PPA(Personal Package Archive)相当于一个软件仓库,与Windows在网上随意抓取EXE安装包不同,PPA里面的软件都是经过审核的. 如何添加PPA? sud ... 
- LeetCode 203
			Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ... 
- CSS实现标题超出宽度省略号来表示
			<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ... 
- c# 远程回收IIS应用池
			利用下列代码可实现IIS应用池的远程回收 var serverManager = ServerManager.OpenRemote(ip); var appPools = serverManager. ... 
- 【转】C#绝对新手之C#中的多线程小结
			大概有4种方法: Dispatcher.异步委托.手动多线程.BackgroundWorker,另外还有一个DispatcherTimer,是定时器. 其中Dispatcher与DispatcherT ... 
- windows API 核心编程学习心得
			一.错误处理 在内部,当windows函数检测到错误时,它会使用“线程本地存储区”的机制将相应的错误代码与“主调线程”关联到一起. winError.h 一般在C:\Program Files\Mic ... 
- OpenGL3-绘制各种图元绘制
			代码下载 #include "CELLWinApp.hpp"#include <gl/GLU.h>#include <assert.h>#include & ... 
- 一个JS内存泄露实例分析
			在看JS GC 相关的文章时,好几次看到了下面这个设计出来的例子,比较巧妙,环环相扣. var theThing = null; var replaceThing = function () { ... 
