Problems
In this problem, when something is said to be chosen at random, it means uniformly at random from among all valid possibilities, and independently of any other choice.
Code Jam contestants once
helped the mighty Goro sort an array of integers.
(You do not need to read that problem to solve this one.) Once again, Goro
needs your help. He has
When Goro bumps the table with his powerful fists, balls pop up in the air and fall back in boxes. Goro can do this so accurately that exactly one ball falls into each box. A ball may fall into the same box it came out of, or into a different one.
Better yet, Goro also has the ability to assign colors to boxes before
each bump. Then, he can bump the table in such a way that balls coming out of
a box of color
For example, suppose the balls appear in the order
- The
1 in the first box falls back into the same box, because that is the only red box. - The
4 and2 in the second and sixth boxes remain in place with probability\frac{1}{2} , and switch places with probability\frac{1}{2} . - The
3, 6, 5 in the third, fourth, and fifth boxes end up in one of the following orders, each with probability\frac{1}{6} :3, 6, 5 3, 5, 6 6, 3, 5 6, 5, 3 5, 3, 6 5, 6, 3
So, for example, the probability of the bump leaving the balls in the
order
Can you help Goro implement a better strategy that will efficiently sort the balls? It is guaranteed that the balls start in a random non-sorted order.