PTA (Basic Level) 1020. Moon cake

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 the market. Then given line N positive number indicates the amount of each stock moon cake (in units of ten thousand tons); The last line gives N positive number indicates a total sales 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
Thinking
  • If something can be split to sell it, of course, is to sell high priced - simple greedy strategy
  • PTA (Advanced Level) 1070 Mooncake is the English version of this title
Code
#include<bits/stdc++.h>
using namespace std;
struct node
{
    double stock;
    double price;
}a[1010];
const double EPS = 1e-7;
bool cmp(node x, node y)
{
    double unit_price1 = x.price * 1.0/ x.stock;
    double unit_price2 = y.price * 1.0/ y.stock;
    return unit_price2 + EPS < unit_price1;
}

int main()
{
    int n, d;
    cin >> n >> d;
    for(int i=0;i<n;i++)
        cin >> a[i].stock;
    for(int i=0;i<n;i++)
        cin >> a[i].price;
    sort(a, a+n, cmp);


    double profit = 0.0;
    for(int i=0;i<n;i++)
    {
        if(a[i].stock <= d)    //对应的货物的库存量 <= 需求量,也就是说这种要全卖
        {
            profit += a[i].price;
            d -= a[i].stock;
        }
        else                   //否则就是库存量 > 需求量,卖一些就行
        {
            profit += (a[i].price * 1.0 / a[i].stock) * d;
            break;
        }
    }
    printf("%.2f\n", profit);
    return 0;
}
Quote

https://pintia.cn/problem-sets/994805260223102976/problems/994805301562163200

Guess you like

Origin www.cnblogs.com/MartinLwx/p/11904262.html