L2-3. Mooncake time limit
Memory Limit 65536 KB
Code length limit 8000 B
Procedures for the award of questions StandardAuthor Chen
Mooncakes are a traditional food eaten by Chinese in the Mid-Autumn Festival, and there are many different flavors of mooncakes in different regions. Now given the stock of all kinds of mooncakes, the total price, as well as the market's maximum demand, please calculate what the maximum benefit you can get.
Note: A portion of the inventory is allowed to be removed during sale. The example gives the following scenario: if we have 3 kinds of mooncakes, the stock is 18, 15, 100,000 tons, the total price is 75, 72, 4.5 billion yuan respectively. If the market's maximum demand is only 200,000 tons, then our maximum profit strategy should be to sell all 150,000 tons of the 2nd kind of mooncakes, and 50,000 tons of the 3rd kind of mooncakes, get 72 + 45/2 = 94.5 (Billion).
Input format:
Each input consists of 1 test cases. Each test case first gives a positive integer no more than 1000 n indicates the number of species of the mooncake, and the positive integer d of not more than 500 (in million tonnes) represents the market's maximum demand. The next line gives an n positive number indicating the stock (in million tonnes) of each mooncake, and the last line gives a positive n indicating the total price of each mooncake (in billions of dollars). The numbers are separated by a space.
Output format:
For each set of test cases, output the maximum profit in a row, in units of billions and accurate to 2 digits after the decimal point.
Input Sample:
3 2018 15 1075 72 45
Sample output:
94.50
Simple greedy, according to the price of a single mooncake, and then priority to take the high value of the moon cakes, incredibly still wrong several hair, moon cakes Inventory and the total price may be a decimal, there may be a decimal sample don't give so regular ah, water a small problem easy ...
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace std;const int MAXN = 1000;struct Node {Double num, Mon, per;} TN[MAXN + 10];bool CMP (node x, node Y) {return x.per > y.per;} int main () {int n;double d;scanf ("%d%lf", &n, &d), for (int i = 0; i < N; i++) {scanf ("%lf", &tn[i].num);} for (int i = 0; i < N; i++) {scanf ("%lf", &tn[i].mon); tn[i].per = Tn[i].mon/tn[i].num;} Sort (tn, TN + N, CMP);d ouble ans = 0;for (int i = 0; i < N; i++) {if (d > Tn[i].num) {D-= Tn[i].num;ans + = Tn[i].mo n;} else {ans + = tn[i].per * d;d = 0;break;}} printf ("%.2f\n", ans); return 0;}
Group programming Ladder-practice set l2-3. Moon Cakes Greedy