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 (排序+贪心)的更多相关文章

  1. BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心

    BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心 Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜 ...

  2. CodeForces 1294B Collecting Packages(排序+贪心)

    http://codeforces.com/contest/1294/problem/B 大致题意: 一张图上有n个包裹,给出他们的坐标,一个机器人从(0,0)出发,只能向右(R)或向上(U),问能否 ...

  3. POJ3687 Labeling Balls(拓扑排序\贪心+Floyd)

    题目是要给n个重量1到n的球编号,有一些约束条件:编号A的球重量要小于编号B的重量,最后就是要输出字典序最小的从1到n各个编号的球的重量. 正向拓扑排序,取最小编号给最小编号是不行的,不举出个例子真的 ...

  4. poj1456 结构体排序+贪心

    题意:给出很多商品,每个商品有价值和出售期限,只能在期限内出售才能获取利润,每一个单位时间只能出售一种商品,问最多能获得多少利润. 只需要按照优先价值大的,其次时间长的排序所有物品,然后贪心选择,从它 ...

  5. poj-2376 Cleaning Shifts (排序+贪心)

    http://poj.org/problem?id=2376 john有n头牛做打扫工作,他想在t时间内每个时间都至少有一头牛在做打扫工作,第一头牛在1,最后一头牛在t时间,每一头牛工作都有一个开始时 ...

  6. vijos 1605 双栈排序 - 贪心 - 二分图

    题目传送门 传送门I 传送门II 题目大意 双栈排序,问最小字典序操作序列. 不能发现两个数$a_{j}, a_{k}\ \ (j < k)$不能放在同一个栈的充分必要条件时存在一个$i$使得$ ...

  7. 2019.01.20 bzoj5158 Alice&Bob(拓扑排序+贪心)

    传送门 短代码简单题. 题意简述:对于一个序列XXX,定义其两个伴随序列a,ba,ba,b,aia_iai​表示以第iii个数结尾的最长上升子序列长度,bib_ibi​表示以第iii个数开头的最长下降 ...

  8. HDU 4857 逃生(反向建边的拓扑排序+贪心思想)

    逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submissi ...

  9. 洛谷P2127 序列排序 [贪心]

    题目传送门 题目描述 小C有一个N个数的整数序列,这个序列的中的数两两不同.小C每次可以交换序列中的任意两个数,代价为这两个数之和.小C希望将整个序列升序排序,问小C需要的最小代价是多少? 输入输出格 ...

随机推荐

  1. 通过 Session 操纵对象

    Session 接口是 Hibernate 向应用程序提供的操纵数据库的最主要的接口, 它提供了基本的保存, 更新, 删除和加载 Java 对象的方法. Session 具有一个缓存, 位于缓存中的对 ...

  2. 关于Linux的总结(三)

    1.man_page.txt 1.内部命令:echo 查看内部命令帮助:help echo 或者 man echo 2.外部命令:ls 查看外部命令帮助:ls --help 或者 man ls 或者 ...

  3. Python_sklearn机器学习库学习笔记(五)k-means(聚类)

    # K的选择:肘部法则 如果问题中没有指定 的值,可以通过肘部法则这一技术来估计聚类数量.肘部法则会把不同 值的成本函数值画出来.随着 值的增大,平均畸变程度会减小:每个类包含的样本数会减少,于是样本 ...

  4. Jersey(1.19.1) - Use of @Context

    Previous sections have introduced the use of @Context. The JAX-RS specification presents all the sta ...

  5. ubuntu忘记密码,忘记root密码的解决方法

    转载于http://forum.ubuntu.org.cn/viewtopic.php?t=272164 ubuntu的root默认是禁止使用的,在安装的时候也没要求你设置root的密码,和红帽系统系 ...

  6. response小结(三)—输出随机图片(验证码功能实现)

    本文是一个很简单的向网页上输出验证码的实现喲!通过注释解释清楚了每一步! Myeclipse下的Package Explorer显示文件结构如下: ResponseDemo.java实现了输出随机图片 ...

  7. SQL中CONVERT()函数用法详解

    SQL中CONVERT函数格式: CONVERT(data_type,expression[,style]) 参数说明: expression 是任何有效的 Microsoft® SQL Server ...

  8. ORACLE多表查询优化

    ORACLE有个高速缓冲的概念,这个高速缓冲就是存放执行过的SQL语句,那oracle在执行sql语句的时候要做很多工作,例如解析sql语句,估算索引利用率,绑定变量,读取数据块等等这些操作.假设高速 ...

  9. Objective-C访问SQLite

    数据库的相关知识我就不去说明了,毕竟只要会sql语言的人就大家都一样. 本案例是在Xcode7.2环境下创建的single view application进行演示操作. 首先第一点,为什么要使用sq ...

  10. block的动态传值例子

    /*  写一个block传值  ,让两个数进行相除和相乘,在运行时动态决定采用哪种计算方式 */ #import <Foundation/Foundation.h> int main(in ...