This page is still under construction.

Parts of this page are still being built. What you see may change.

Urns

Time limit1sMemory limit128 MB

Summary
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

  • 60100×12=7+20100\frac{60}{100} \times 12 = 7 + \frac{20}{100} red balls, and
  • 40100×12=4+80100\frac{40}{100} \times 12 = 4 + \frac{80}{100} 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,

∣7−(7+20100)∣+∣5−(4+80100)∣=20100+20100=40100,\left|7 - \left(7 + \frac{20}{100}\right)\right| + \left|5 - \left(4 + \frac{80}{100}\right)\right| = \frac{20}{100} + \frac{20}{100} = \frac{40}{100},

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 (r,o,y,g,b)(r, o, y, g, b) and take the smallest one in dictionary order to break such a tie. Here the candidates are (1,0,0,1,0)(1, 0, 0, 1, 0), (1,0,0,0,1)(1, 0, 0, 0, 1) and (0,0,0,1,1)(0, 0, 0, 1, 1), so the choice is (0,0,0,1,1)(0, 0, 0, 1, 1).

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.

Examples2

  1. Example 1

    Input
    No Blue
    100 20 50 30 5
    50 1 2
    20 1 3
    17 4 2
    31 3 1
    0 0 0
    All Blue
    1 1 1 1 99999
    2 1 2
    0 0 0
    Well Mixed
    1 1 1 1 1
    1 1 2
    2 2 3
    3 3 4
    4 4 5
    0 0 0
    #
    
    Expected output
    No Blue
    URN        R      O      Y      G      B
    1         39      0     22      0      0
    2         50     20      0     17      0
    3         11      0     28      0      0
    4          0      0      0     13      0
    5          0      0      0      0      5
    
    All Blue
    URN        R      O      Y      G      B
    1          0      0      0      0      0
    2          1      1      0      0      0
    3          0      0      1      0      0
    4          0      0      0      1      0
    5          0      0      0      0  99999
    
    Well Mixed
    URN        R      O      Y      G      B
    1          0      0      0      0      0
    2          0      0      0      0      0
    3          0      0      0      0      0
    4          0      0      0      0      0
    5          1      1      1      1      1
    
  2. Example 2

    Input
    Tie Break
    50 0 0 50 50
    50 4 1
    50 5 1
    2 1 2
    0 0 0
    #
    
    Expected output
    Tie Break
    URN        R      O      Y      G      B
    1         50      0      0     49     49
    2          0      0      0      1      1
    3          0      0      0      0      0
    4          0      0      0      0      0
    5          0      0      0      0      0