zoukankan      html  css  js  c++  java
  • PAT1070:Mooncake

    1070. Mooncake (25)

    时间限制
    100 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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
    

    思路

    简单的贪心算法,求出每种月饼的单价,按单价的从高到低顺序去满足需要的月饼数就可以得到最大利润。

    注:当需求超过总月饼时,月饼的索引index应不大于N - 1,也就是说将所有月饼的利润加起来就行,之前没注意检测这一边界条件导致段错误。

    代码

    #include<iostream>
    #include<iomanip>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    class mooncake
    {
      public:
        float amount;
        float profit;
        float price;
    };
    
    bool compare(mooncake a,mooncake b)
    {
        return a.price > b.price;
    }
    
    int main()
    {
        int N;
        float demand;
        while(cin >> N >> demand)
        {
            vector<mooncake> cakes(N);
            for(int i = 0;i < N;i++)
            {
                cin >> cakes[i].amount;
            }
            for(int i = 0;i < N;i++)
            {
                cin >> cakes[i].profit;
                cakes[i].price = cakes[i].profit/cakes[i].amount;
            }
            sort(cakes.begin(),cakes.end(),compare);
            int index = 0;
            float sum = 0;
            while(demand > 0 && index <= N - 1) //demand很大时index会越界,要注意判断下。
            {
               if(demand - cakes[index].amount > 0)
               {
                   sum += cakes[index].profit;
               }
               else
               {
                   sum += demand * cakes[index].price;
               }
               demand -= cakes[index++].amount;
            }
            cout << fixed << setprecision(2) << sum << endl;
        }
    }
  • 相关阅读:
    如何提高英阅读英文技术资料
    如何阅读英语文章
    vijosP1223麦森数
    vijosP1359 Superprime
    vijosP1319 数列
    vijosP1447 开关灯泡
    vijosP1164 曹冲养猪
    vijosP1016 北京2008的挂钟
    洛谷P1457 城堡(The Castle)
    洛谷P1294 高手去散步
  • 原文地址:https://www.cnblogs.com/0kk470/p/7699344.html
Copyright © 2011-2022 走看看