1. 程式人生 > 其它 >1070 Mooncake (25 分)(貪心演算法)

1070 Mooncake (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 regions 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

生詞

英文 解釋
bakery 烘烤產品
fillings and crusts 餡料和麵皮
inventory 清單,庫存
partial 部分的

題目大意:

N表示月餅種類,D表示月餅的市場最大需求量,給出每種月餅的數量和總價,問根據市場最大需求量,這些月餅的最大銷售利潤為多少~

分析:

首先根據月餅的總價和數量計算出每一種月餅的單價,然後將月餅陣列按照單價從大到小排序,根據需求量need的大小,從單價最大的月餅開始售賣,將銷售掉這種月餅的價格累加到result中,最後輸出result即可~

原文連結:https://blog.csdn.net/liuchuo/article/details/51985849

23分答案

樣例2沒過,,,

#include <bits/stdc++.h>

using namespace std;

struct node
{
    double amount,total,unit;
};
bool cmp(node a,node b)
{
    return a.unit>b.unit;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int n;
    double d;
    cin>>n>>d;
    vector<node> v(n);
    for(int i=0;i<n;i++){
        cin>>v[i].amount;
    }
    for(int i=0;i<n;i++){
        cin>>v[i].total;
        v[i].unit=v[i].total/v[i].amount;
    }
    sort(v.begin(),v.end(),cmp);
    double cnt=0,sum=0;
    for(int i=0;i<n;i++){
        if(cnt<=d){
            cnt+=v[i].amount;
            sum+=v[i].total;
        }else {
            sum-=v[i-1].unit*(cnt-d);
            break;
        }
    }
    printf("%.2f\n",sum);
    return 0;
}

柳神答案

#include <bits/stdc++.h>

using namespace std;

struct node
{
    double amount,total,unit;
};
bool cmp(node a,node b)
{
    return a.unit>b.unit;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int n;
    double d;
    cin>>n>>d;
    vector<node> v(n);
    for(int i=0;i<n;i++){
        cin>>v[i].amount;
    }
    for(int i=0;i<n;i++){
        cin>>v[i].total;
        v[i].unit=v[i].total/v[i].amount;
    }
    sort(v.begin(),v.end(),cmp);
    double sum=0;
    for(int i=0;i<n;i++){
        if(v[i].amount<=d){
            sum+=v[i].total;
        }else {
            sum+=v[i].unit*d;
            break;
        }
        d-=v[i].amount;
    }
    printf("%.2f\n",sum);
    return 0;
}

本文來自部落格園,作者:勇往直前的力量,轉載請註明原文連結:https://www.cnblogs.com/moonlight1999/p/15599206.html