1070 Mooncake (25)(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:

  1. 3 200
  2. 180 150 100
  3. 7.5 7.2 4.5

Sample Output:

  1. 9.45

题目大意:给出月饼的需求量,并且给出每种月饼的总量及其总价格,要求按需求给出最大利润。

代码来自:https://www.liuchuo.net/archives/1912

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. struct mooncake{
  6. float mount, price, unit;
  7. };
  8. int cmp(mooncake a, mooncake b) {
  9. return a.unit > b.unit;
  10. }
  11. int main() {
  12. int n, need;
  13. cin >> n >> need;
  14. vector<mooncake> a(n);//定义大小为n的向量
  15. for (int i = ; i < n; i++)
  16. cin >> a[i].mount;
  17. for (int i = ; i < n; i++)
  18. cin >> a[i].price;
  19. for (int i = ; i < n; i++)
  20. a[i].unit = a[i].price / a[i].mount;//计算单位价格
  21. sort(a.begin(), a.end(), cmp);//按照单价进行排序。
  22. float result = 0.0;
  23. for (int i = ; i < n; i++) {
  24. if (a[i].mount <= need) {
  25. result = result + a[i].price;
  26. } else {
  27. result = result + a[i].unit * need;
  28. break;
  29. }
  30. need = need - a[i].mount;
  31. }
  32. printf("%.2f",result);
  33. return ;
  34. }

//不是贪心问题啊,就是一个普通的题目,对单位进行排序。 不要被贪心吓到了啊。

PAT 1070 Mooncake[一般]的更多相关文章

  1. PAT 1070. Mooncake (25)

    Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival.  Many types ...

  2. PAT 1070 Mooncake

    题目意思能搞成这样我也是服了这个女人了 #include <cstdio> #include <cstdlib> #include <vector> #includ ...

  3. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

  4. 1070 Mooncake (25 分)

    1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn ...

  5. 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

  6. PAT Advanced 1070 Mooncake (25) [贪⼼算法]

    题目 Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many typ ...

  7. PAT (Advanced Level) 1070. Mooncake (25)

    简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vecto ...

  8. PAT甲题题解-1070. Mooncake (25)-排序,大水题

    #include <iostream> #include <cstdio> #include <algorithm> #include <string.h&g ...

  9. 1070. Mooncake (25)

    题目如下: Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many ...

随机推荐

  1. 【线程】Thread中的join介绍

    因为sleep.wait.join等阻塞,可以使用interrupted exception异常唤醒. 一.作用 Thread类中的join方法的主要作用就是同步,它可以使得线程之间的并行执行变为串行 ...

  2. ant重新编译打包hadoop-core-1.2.1.jar时遇到的错

    错误1. [root@MyDB01 hadoop]# ant -Dversion=1.2.1 examples 错误: 找不到或无法加载主类 org.apache.tools.ant.launch.L ...

  3. MySQL里面的子查询

    一.子查询定义 定义: 子查询允许把一个查询嵌套在另一个查询当中. 子查询,又叫内部查询,相对于内部查询,包含内部查询的就称为外部查询. 子查询可以包含普通select可以包括的任何子句,比如:dis ...

  4. Linux下进程隐藏的方法及其对抗

    零.背景 在应急响应中,经常碰到ps命令和top命令查不到恶意进程(异常进程)的情况,会对应急响应造成很大的影响.轻则浪费时间,重则排查不出问题,让黑客逍遥法外.所以这篇博客研究学习如何对抗linux ...

  5. 上传控件---淘宝kissy uploader+瀑布流显示

    介绍Uploader : Uploader 是由阿里集团前端工程师们发起创建的一个开源 JS 框架.它具备模块化.高扩展性.组件齐全,接口一致.自主开发.适合多种应用场景等特性. Uploader是非 ...

  6. linux编码问题小节

    今天又碰到了难缠的python编码问题,首先主要还是linux操作系统中的编码问题. 无论怎么样,我都没办法在linux的vim中利用中文输入法打出中文? vim中的set encoding,set ...

  7. 使用shell数据处理数据实例①-------手把手教学版

    引子: 在工作过程中经常要处理各种小数据,同事间会用各种工具方法来处理,比如用java.python.Perl甚至用UE手工处理.但貌似不都方便. 今天举一例子使用shell来处理,来说明shell一 ...

  8. iOS - Block的循环引用内存泄漏问题探索

    循环引用的原因 众所周知,ARC下用block会产生循环引用的问题,造成泄露的原因是啥呢? 最简单的例子,如下面代码: [self.teacher requestData:^(NSData *data ...

  9. 9.20 session

    2018-9-20 13:14:40 Cookie session 参考 :http://www.cnblogs.com/liwenzhou/p/8343243.html 类视图  cbv 注册 ur ...

  10. Python 重定向获取真实url

    通常的返回url: http_headers = { 'Accept': '*/*','Connection': 'keep-alive', 'User-Agent': 'Mozilla/5.0 (W ...