PAT A1070 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).

月饼是在中秋节吃的一种中国烘焙产品。根据该地区的文化,传统月饼中可以找到许多种类的馅料和面包皮。现在,考虑到各种种类月饼的库存量和价格以及市场的最大需求量,你需要告诉能够获得的最大利润。

注意:销售时允许取出一部分库存。示例显示以下情况:给出三种月饼,库存分别是18,15和10万吨,总价格分别是75,72和45亿元。如果市场需求量最多为20万吨,我们最好能够买掉15万吨的第二种月饼和5万吨的第三种月饼,总利益是7.2+4.5/2=94.5亿

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.

每个测试样例第一行包含2个正整数N(≤1000)--月饼的种类 和D(≤50万吨)--市场最大需求量。

第二行给出N种月饼的正的库存数量(以万吨为单位),第三行给出正的价格(以亿元为单位)。

Output Specification:

For each test case, print the maximum profit (in billion yuans) in one line, accurate up to 2 decimal places.

输出最大利润(以亿元为单位),精确到小数点后两位

思路:这里采用"总是选择单价最高的月饼出售,可以获得最大的利润"的策略

#include<cstdio>
#include<algorithm>
using namespace std;
struct mooncake{
	double store;//库存量
	double sell;//总售价
	double price;//单价 
}cake[1010];

bool cmp(mooncake a,mooncake b){
	return a.price>b.price;
}
int main(){
	int n;
	double D;
	scanf("%d%lf",&n,&D);
	for(int i=0;i<n;i++){
		scanf("%lf",&cake[i].store);
	}
	for(int i=0;i<n;i++){
		scanf("%lf",&cake[i].sell);
		cake[i].price=cake[i].sell/cake[i].store;
	}
	sort(cake,cake+n,cmp);
	double ans=0;//收益
	for(int i=0;i<n;i++){
		if(cake[i].store<D){
			D-=cake[i].store;
			ans+=cake[i].sell;
		}else{
			ans += cake[i].price*D;
			break;
		}
	} 
	printf("%.2f\n",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38179583/article/details/86598918