Little Monkeys
n little monkeys jumping on the bed,
Pick a random monkey, the instructions read
Off the sleepy trampoline, the monkey fled
One less monkey jumping on the bed
There are little monkeys jumping on the bed. You've devised a program that can randomly select monkeys to fall off the bed, so you are left with little monkeys jumping on the bed.
You've decided to model this by assigning a positive weight to each of the monkeys. For example you might start with the following monkey names and associated weights:
names = ["George", "Kong", "Rafiki", "Caesar", "Koko"]
weights = [ 4, 2, 3, 1, 5 ]
Then, for a list of size we generate random numbers between 0 and 1. We then use these random numbers to compute the next distinct monkey in the sequence bit by bit in the following manner. Suppose the 4 random numbers we generated was 0.8, 0.2, 0.5, 0.8.
Iteration 1
- First, we sum up all of the weights in the current list (4+2+3+1+5=15), and multiply this by your random number (0.8*15=12)
- Next, we find the first remaining element whose left partial sum is larger or equal to the computed value (, but , so "Koko" is selected)
- The selected item and weight is removed from the list and added to the order
The left partial sum is the result of summing all weights left of the associated name. So the left partial sum of "Rafiki" is 4+2+3=9.
Current remaining list:
names = ["George", "Kong", "Rafiki", "Caesar"]
weights = [ 4, 2, 3, 1 ]
ordering = ["Koko"]
Iteration 2
- Summing up the remaining weights gives 10, multiplying by 0.2 we get 2.
- , so "George" is selected.
- This is removed from our values and added to the current order.
names = ["Kong", "Rafiki", "Caesar"]
weights = [ 2, 3, 1 ]
ordering = ["Koko", "George"]
Iteration 3
- Summing up the weights gives 6, multiplying by 0.5 gives 3
- but , so "Rafiki" is selected
- This is removed from our values and added to the current order.
names = ["Kong", "Caesar"]
weights = [ 2, 1 ]
ordering = ["Koko", "George", "Rafiki"]
Iteration 4
- Summing up the weights gives 3, multiplying by 0.8 gives 2.4
- but , so "Caesar" is selected
- This is removed from our values and added to the current order.
- And since only "Kong" is left, it is added to the end of the ordering, with no random number required.
ordering = ["Koko", "George", "Rafiki", "Caesar", "Kong"]
Your problem is, given the monkey names, weights, and the random numbers generated, generate the order of monkeys.
Input
Input will start with a single integer , the number of monkeys on the bed.
The next line will contain space separated strings, these are the names of the monkeys.
The next line will contain space separated integers, these are the weights of the monkeys.
The next line will contain space separated floating point values, these are the random numbers generated.
Output
Output space separated strings representing the correct ordering.
Constraints
- Each string only contains characters from
a-z
andA-Z
, and is at most 6 characters long - You may assume that changing the random numbers by will not change the result (IE you can ignore floating point shenanigans, with enough precision)
- All values are less than or equal to
Test Set A: 12.5%
Test Set B: 87.5%
Example
For input:
5
George Kong Rafiki Caesar Koko
4 2 3 1 5
0.8 0.2 0.5 0.8
The output should be:
Koko George Rafiki Caesar Kong
For the reasoning outlined earlier in the statement.
Comments