PAT B 1020 Mooncake (c + + edition)

Source: Internet
Author: User

1020. Mooncake (25) time limit of MS memory limit 65536 KB code length limit 8000 B procedure StandardAuthor Chen, Yue

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

IDEA: Using map mapping and incremental features, we use the unit price to map the total and total price of mooncakes

Note: The total and total price is positive, not necessarily positive, and the price of the moon cake may be the same, so we use the Multimap container


1 //1020.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6#include <map>7#include <iomanip>8 9 using namespacestd;Ten  One struct Food A { -     DoubleTotal//Total -     DoubleTotal_price;//Total Price the }; -  - voidGet_money (multimap<Double, food>& m,intD);//Obtain the total price and print - voidInput (multimap<Double, food>& m, food p[],intN);//enter information into the container +  - intMain () + { A     intn,d; atmultimap<Double, food>m; -  -CIN >> N >>D; -  -Food *p =NewFood[n]; -  inInput (M, p, N);//enter information into the container -  toGet_money (M, D);//Obtain the total price and print +  -     Delete[] p; the  *     return 0; $ }Panax Notoginseng  - //enter information into the container the voidInput (multimap<Double,food>& M,food p[],intN) + { A      for(inti =0; i < N; ++i) theCIN >>p[i].total; +  -      for(inti =0; i < N; ++i) $     { $CIN >>P[i].total_price; -  -         //map mooncakes with unit price theM.insert (pair<Double, Food> (p[i].total_price/p[i].total*1.0, P[i])); -     }Wuyi } the  - //Obtain the total price and print Wu voidGet_money (multimap<Double, food>& m,intD) - { About     Doublesum =0; $  -     //Reverse Iterator -multimap<Double, Food>::reverse_iterator I=m.rbegin (), end =m.rend (); -  A      while(D >0&&i!=end) +     { the         //sufficient Demand -         if(I->second.total <D) $         { theSum + = i->Second.total_price; theD-= i->second.total; the  the++i; -         } in         Else//The remaining demand is less than the total amount of such mooncakes the         { theSum + = d* (i->First ); About  theB0; the         } the     } +  -cout << Setiosflags (iOS::fixed) <<setprecision (2) << sum <<Endl; the}

PAT B 1020 Mooncake (c + + edition)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.