PAT Basic 1020 moon cake (25 points)

Chinese people eat moon cake is in the Mid-Autumn Festival a traditional food, different regions have many different flavors of moon cake. Now given all kinds of moon cake inventory, total price, and the maximum demand of the market, you can get the maximum benefit calculated how much.

Note: Allow removing a portion of the stock sale. Case examples given is this: If we have three kinds of moon cake, its stocks were 18,15,10 tons, the total price respectively 75,72,45 billion. If the maximum market demand is only 20 million tons, then we should be the maximum revenue strategy to sell all of the 150,000 tons of the two kinds of moon cake, as well as 50,000 tons of the three kinds of moon cake, get 72 + 45/2 = 94.5 (million) .

Input formats:

Each input comprises a test. Each test case is given to a positive integer not more than 1000  N represents the number of types of moon cake, and no more than 500 (in units of thousands of tons) of the positive integer  D represents the maximum demand of market. Then given line  N represents a positive number of stocks of each moon cake (in units of thousands of tons); The last line gives  the N represents a positive number in the total price of each moon cake (in units of billions). Between numbers separated by a space.

Output formats:

For each test, the maximum benefit in the output line, in units of billions and accurately to two decimal places.

Sample input:

3 20
18 15 10
75 72 45

Sample output:

94.50


#include <iostream>
#include <algorithm>
using namespace std;
struct mooncake{
    double allPrice;
    double amount;
    double unitPrice;
};
bool cmp(mooncake a,mooncake b){
    return a.unitPrice>b.unitPrice;
}
int main(){
    int coun,need;
    double price=0;
    cin>>coun>>need;
    mooncake mc[coun];
    for(int i=0;i<coun;i++){
        cin>>mc[i].amount;
    }
    for(int i=0;i<coun;i++){
        cin>>mc[i].allPrice;
        mc[i].unitPrice=(mc[i].allPrice/mc[i].amount);
    }
    sort(mc,mc+coun,cmp);
    for(int i=0;i<coun;i++){
        if((need-mc[i].amount)>=0){
            need-=mc[i].amount;
            price+=mc[i].allPrice;
        }else{
            price+=(need*mc[i].unitPrice);
            break;
        }
    }
    printf("%.2f",price);
    system("pause");
    return 0;
}

 





Guess you like

Origin www.cnblogs.com/littlepage/p/11355713.html