Decide whether whole numbers of up to eight drinks can total exactly m in cost and u in alcohol, and print the lexicographically smallest purchase.
Medium6Dynamic programmingMathNo attempts yetTime limit1sMemory limit256 MBThe University of Lagado runs a beer tasting during Fresher's week. For safety the university caps how much money one student may spend, and each student sets a personal limit on the alcohol they will drink that night.
Drinks are served in one of three sizes: a litre, half a litre, or a third of a litre. One unit of alcohol is the amount contained in one litre of a drink of strength 1 percent. A drink of strength s percent served in a size of f litres therefore holds s×f units.
A student wants to spend all m of their money, down to the last coin, and drink exactly u units of alcohol. No more and no less. Any drink may be bought any number of times, including none. Decide whether such a purchase exists, and if it does, print what to buy and how many of each.
The first line holds three values.
Each of the next d lines holds four values separated by single spaces.
1/1 for a litre, 1/2 for half a litre, 1/3 for a third of a litre.If no purchase spends exactly m and delivers exactly u units, print one line holding IMPOSSIBLE.
Otherwise print one line for every drink bought, in the order the drinks appear in the input, giving the name and then the number bought, separated by one space. A drink bought zero times is not printed.
Several purchases may work. Write ci for the number bought of the i-th drink of the input, so that a purchase is the sequence (c1,c2,…,cd), and print the purchase that comes first in lexicographic order: take the smallest c1, among those the smallest c2, and so on to the end.
Because m is at least 0.01, any purchase that exists buys at least one drink, so the output is never empty.