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 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
题目描述:月饼吧啦吧啦吧啦,直接分析样例,假设库存的月饼种数为3,三种月饼库存分别有180、150、100(千吨),他们对应的总价值分别是7.5、7.2、4.5百万元。若现在市场的月饼需求量是200(千吨),求出商家可赚的最大利润是多少。
输入:第一行包括两个数,n--月饼种数,D--市场的月饼需求量。第二行有n个数,分别表示n种月饼的库存量。第三行有n个数,分别表示n种月饼各自的总价值。
输出:商家可赚的最大总利润。
解题思路:每种月饼的参数都存放到结构体中,还要求出每种月饼的单品价格。将结构体数组根据单价进行排序,单品价格高,那商家所得利润也比较高是不。排好序,根据市场需求量进行购买计算。很简单的就能得到商家获得的最高总利润啦。
注意点:测试点2,月饼的库存量要用浮点数来存啊!!!
#include<bits/stdc++.h>
using namespace std;
struct node{
double amount, price, avg;
};
vector<node> v;
bool cmp(node a, node b){
return a.avg > b.avg;
}
int main()
{
int n;
double ans=0, d;
node t;
scanf("%d %lf", &n, &d);
v.resize(n);
for(int i=0; i<n; i++) scanf("%lf", &v[i].amount);
for(int i=0; i<n; i++){
scanf("%lf", &v[i].price);
v[i].avg = v[i].price/v[i].amount;
}
sort(v.begin(),v.end(),cmp);
for(int i=0; i<v.size()&&d>0; i++){
t = v[i];
if(t.amount<=d){
d -= t.amount;
ans += t.price;
}
else{
ans += t.avg*d;
d = 0;
}
}
printf("%.2f\n", ans);
return 0;
}