MixingLiquids
SRM 355 · 2007-06-20 · by Petr
Problem Statement
In Chemistry, there's a different meaning to the word 'solution' than in programming. When we mix x liters of some substance with (100-x) liters of water, we get 100 liters of x-% solution of that substance.
You are given several bottles containing solutions of the same substance. The i-th bottle contains amount[i] liters of percent[i]-% solution. Return the maximal number of liters of need-% solution we can get by pouring together some of these bottles (possibly partially, see example 0).
Notes
- The returned value must have an absolute or relative error less than 1e-9.
Constraints
- percent will contain between 1 and 50 elements, inclusive.
- Each element of percent will be between 0 and 100, inclusive.
- amount will contain the same number of elements as percent.
- Each element of amount will be between 1 and 1000, inclusive.
- need will be between 0 and 100, inclusive.
{0, 100}
{20, 30}
50
Returns: 40.0
We have 20 liters of water and 30 liters of pure substance. We need a 50% solution, so we combine all the water with 20 liters of substance.
{0, 100}
{20, 30}
60
Returns: 50.0
We can use everything we have.
{90, 70, 80}
{10, 10, 10}
50
Returns: 0.0
All our bottles contain too much substance.
{30, 80, 60}
{40, 10, 15}
57
Returns: 35.18518518518519
{27,72,40,2}
{117,606,421,523}
28
Returns: 1257.8863636363637
Submissions are judged against all 165 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class MixingLiquids with a public method double howMuch(vector<int> percent, vector<int> amount, int need) · 165 test cases · 2 s / 256 MB per case