【一隻蒟蒻的刷題歷程】 【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;
}


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