【一只蒟蒻的刷题历程】 【PAT】 A1070 月饼

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


题目大意:

月饼是中国的烘焙产品,传统上是在中秋节期间食用的。根据该地区的文化,在传统的月饼中可以找到许多类型的馅料和硬皮。现在,考虑到各种月饼的库存量和价格,以及最大的市场总需求,您应该说出可以赚到的最大利润。

注意:可以采用部分库存存储。样本显示以下情况:给定三种月饼,库存量分别为180、150和10万吨,价格分别为7.5、7.2和45亿元。如果市场需求最多为20万吨,那么我们最好的办法就是出售第二种月饼15万吨,第三种月饼5万吨。因此,总利润为7.2 + 4.5 / 2 = 9.45(十亿人民币)。

输入规格:

每个输入文件包含一个测试用例。对于每种情况,第一行包含2个正整数N(≤1000)(不同种类的月饼的数量)和D(≤50万吨),即市场的最大总需求。然后第二行给出了正数的库存量(千吨),第三行给出了N种月饼的正价格(十亿人民币)。一行中的所有数字都用空格分隔。

输出规格:

对于每个测试用例,一行打印最大利润(十亿人民币),精确到小数点后两位。

样本输入:

3 200
180150100
7.5 7.2 4.5

样本输出:

9.45


思路:

贪心,把单价高的先卖掉

代码:

#include <iostream>
#include <algorithm> 
using namespace std;
struct yuebing{
	double zong; //总价格 
	double kucun; //库存 
	double price;  //单价 
}moon[1005];
bool cmp(yuebing a,yuebing b) //单价从大到小排序 
{
	return a.price>b.price;
 } 
int main()
{
   int n,d;
   cin>>n>>d;
   for(int i=0;i<n;i++)
      cin>>moon[i].kucun;
   for(int i=0;i<n;i++) 
   {
   	cin>>moon[i].zong;
   	moon[i].price = moon[i].zong / moon[i].kucun; //计算单价 
   }
      
    sort(moon,moon+n,cmp); /*单价由高到低*/ 
	
	double ans=0;
	for(int i=0;i<n;i++)
	{
		if(d>=moon[i].kucun) //需求大于等于该月饼库存 
		{
			d-=moon[i].kucun;  //库存全部拿出 
		    ans+=moon[i].zong; //价格加上该月饼库存的总价 
		}
		else   //需求 小于库存 
		{
			ans+=d*moon[i].price; //只拿需求的那么多月饼 
			break;     //结束循环 
		}
		  
	  }  
   printf("%.2f",ans);  //按格式输出 
   return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章