Urns
Time limit1sMemory limit128 MB
You move balls between five urns in proportion to their color shares and print the final counts.
- Level
Medium6 of 10
- Topics
- Simulation, Math, Implementation
- Solved
- No attempts yet
Problem
You start with five urns, and each one holds balls of a single colour. Urn 1 holds red, urn 2 orange, urn 3 yellow, urn 4 green, and urn 5 blue. Balls are then moved from urn to urn. Report the contents of every urn once all the moves are done.
An urn is mixed thoroughly before each move, so thoroughly that the number of balls moved of each colour matches, as closely as possible, that colour's share of the source urn just before the move.
Take an urn holding 60 red balls and 40 green balls. Moving 10 balls moves exactly 6 red and 4 green. Moving 12 balls would ideally move
- red balls, and
- green balls,
but a count has to be a whole number. Here 7 red and 5 green move, because the discrepancy from the ideal amounts,
is smaller than the discrepancy of any other choice.
Two choices can produce the same discrepancy. If an urn holds 50 red, 50 green and 50 blue balls and two balls are drawn, picking one ball of any two different colours gives the same discrepancy every time. Write a choice as a sequence and take the smallest one in dictionary order to break such a tie. Here the candidates are , and , so the choice is .
If a move asks for more balls than the source urn holds, all of its balls move.
Input
The input contains several trials. Each trial starts with a line holding the name of the trial. The next line holds the initial contents of the five urns in order, five integers between 0 and 99999. Lines of three integers follow. The first is the number of balls to move, the second is the number of the source urn (1 to 5), and the third is the number of the target urn. A line of three zeroes (0 0 0) ends a trial. A line holding a single # ends the input.
Output
For each trial print the name of the trial, then the result for that trial. The result starts with a heading line: the word URN, eight spaces, then the letters R, O, Y, G and B with six spaces between consecutive letters. The next five lines give the final contents of urns 1 to 5 in that order. Each line starts with the urn number, then four spaces, then the five colour counts of that urn, each right justified in a field of width seven. Print a blank line between consecutive trials.