1020. 月餅 (25)
月餅是中國人在中秋佳節時吃的一種傳統食品,不同地區有許多不同風味的月餅。現給定所有種類月餅的庫存量、總售價、以及市場的最大需求量,請你計算可以獲得的最大收益是多少。
注意:銷售時允許取出一部分庫存。樣例給出的情形是這樣的:假如我們有3種月餅,其庫存量分別爲18、15、10萬噸,總售價分別爲75、72、45億元。如果市場的最大需求量只有20萬噸,那麼我們最大收益策略應該是賣出全部15萬噸第2種月餅、以及5萬噸第3種月餅,獲得 72 + 45/2 = 94.5(億元)。
輸入格式:
每個輸入包含1個測試用例。每個測試用例先給出一個不超過1000的正整數N表示月餅的種類數、以及不超過500(以萬噸爲單位)的正整數D表示市場最大需求量。隨後一行給出N個正數表示每種月餅的庫存量(以萬噸爲單位);最後一行給出N個正數表示每種月餅的總售價(以億元爲單位)。數字間以空格分隔。
輸出格式:
對每組測試用例,在一行中輸出最大收益,以億元爲單位並精確到小數點後2位。
輸入樣例:3 20 18 15 10 75 72 45輸出樣例:
94.50
/***************************************************88
@ mail:[email protected]
@ author:GaominQuan
*******************************************************/
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
using namespace std;
class cake{
public:
double number;
double totalPrice;
double perPrice;
/*
NOTICE THE TYPE!!!
*/
cake(double num){
number = num;
};
void setPrice(double price){
totalPrice = price;
if(number == 0){
perPrice = 0;
}else{
perPrice = totalPrice*1.0/number;
}
};
};
inline bool compare(cake c1, cake c2){
return c1.perPrice > c2.perPrice;
}
double choose(vector<cake> allCakes,int MAX){
double totalPrice = 0;
double totalWeight = 0;
double remainWeight = MAX;
for(int iCake = 0;iCake<allCakes.size();iCake++){
if(allCakes[iCake].number <= remainWeight){
totalPrice += allCakes[iCake].totalPrice;
remainWeight -= allCakes[iCake].number;
}else{
totalPrice += (1.0*remainWeight/allCakes[iCake].number)*allCakes[iCake].totalPrice;
break;
}
}
return totalPrice;
}
int main(){
int KINDS = 0, MAX = 0;
cin>>KINDS>>MAX;
vector<cake> allCakes;
for(int nIndex = 0;nIndex<KINDS; nIndex++){
double number = 0;
cin>>number;
cake newCake(number);
allCakes.push_back(newCake);
}
for(int pIndex = 0; pIndex < KINDS; pIndex++){
double price = 0;
cin>>price;
allCakes[pIndex].setPrice(price);
}
sort(allCakes.begin(),allCakes.end(),compare);
printf("%.2f\n",choose(allCakes,MAX));
return 0;
}