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 structural and sequencing application questions
1#include <iostream>2#include <stdio.h>3#include <algorithm>4 using namespacestd;5 structnode6 {7 DoubleSum,zongjia,danjia;8}yuebing[1005];9 BOOLCMP (node A,node b)Ten { One returnA.danjia>B.danjia; A }; - intMain () - { the intN,MAXN; -Cin>>n>>MAXN; - inti; - for(i=0; i<n;i++) + { -Cin>>yuebing[i].sum; + } A for(i=0; i<n;i++) at { -Cin>>Yuebing[i].zongjia; - } - for(i=0; i<n;i++) - { -yuebing[i].danjia=yuebing[i].zongjia/yuebing[i].sum; in } -Sort (yuebing,yuebing+n,cmp); to Doubleres=0.0; + for(i=0; i<n;i++) - { the if(maxn<=0) Break; * if(maxn>=yuebing[i].sum) $ {Panax Notoginsengmaxn-=yuebing[i].sum; -res+=Yuebing[i].zongjia; the } + Else if(maxn<yuebing[i].sum) A { theres+=maxn*Yuebing[i].danjia; +maxn=0; - } $ } $printf"%.2lf\n", res); - return 0; -}
PAT Group Programming Ladder-practice set l2-003. Moon cakes