zoukankan      html  css  js  c++  java
  • 1070 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 (≤), the number of different kinds of mooncakes, and D (≤ 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

    题意:

      根据库存,和这些库存对应的价格,以及所要购买的总量,输出最大收益。

    思路:

      求出单价,然后对单价进行降序排列,然后购买,直到购得的总量满足要求。(第一次提交的时候有一组测试点没有通过,后来看了别人的blog发现库存也应该用double来表示)

    Code:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 struct Mooncake {
     6     double amount = 0.0;
     7     double profit = 0.0;
     8     double price = 0.0;
     9 };
    10 
    11 bool cmp(Mooncake a, Mooncake b) { return a.price > b.price; }
    12 
    13 int main() {
    14     int n, total;
    15     cin >> n >> total;
    16     vector<Mooncake> v(n);
    17     for (int i = 0; i < n; ++i) cin >> v[i].amount;
    18     for (int i = 0; i < n; ++i) {
    19         cin >> v[i].profit;
    20         v[i].price = v[i].profit / v[i].amount;
    21     }
    22     sort(v.begin(), v.end(), cmp);
    23     double sumProfit = 0.0;
    24     for (int i = 0; i < n; ++i) {
    25         if (total >= v[i].amount) {
    26             sumProfit += v[i].profit;
    27             total -= v[i].amount;
    28         } else {
    29             sumProfit += v[i].profit * ((double)total / v[i].amount);
    30             break;
    31         }
    32     }
    33     cout << fixed << setprecision(2) << sumProfit << endl;
    34     return 0;
    35 }
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    详解threading模块:Condition类的使用
    concurrent.futures 模块使用说明
    细读Spring源码(四)什么是IOC?
    OAuth 2.0系列(六) OAuth令牌
    OAuth 2.0系列(七) OAuth 2.0可能存在的安全漏洞及预防方法
    细读Spring源码(一)refresh()方法概览
    HashMap源码解读() put方法的实现过程
    跟着字节码追踪JAVA中的基本运算
    细读Spring源码(六)Spring源码设计架构
    细读Spring源码(五)AOP从实战到源码
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12823506.html
Copyright © 2011-2022 走看看