You and a friend are playing a game - who can overflow the most containers? There are n
containers numbered 1 to n
. Each container has a certain maximum capacity of water, in litres.
You take turns with your friend to pour discrete amounts of water into the containers. On your turn, you may pour 2 litres of water into one container (you can't distribute into multiple containers), and on your friend's turn, he must pour 3
litres. You may also choose, on your turn, to not pour water in any of the containers (you cannot, however, choose to only pour 1 litre). The person who manages to overflow the container - that is, after they finish their turn, the litres of water inside the container is greater than its maximum capacity - gets a point.
The input comes in the form of space separated integers. The leftmost one is labelled 1 and the rightmost one is labelled n
. For instance:
Y 2 1 4 3
Note that the letter indicates who goes first. Y
means that you go first, and F
means that your friend goes first. The 2 litre container is labelled 1
, the 1 litre container is labelled 2
, etc. So we would call the 2 litre container "Container 1", because it is labelled 1
.
Now, your friend has a very naïve strategy. He always pours all his 3 litres of water into the container that is closest to overflowing. If there is more than one container that is equally closest to overflowing, he will pour all his water in the one that is labelled with the lowest number.
Suppose I use the same naïve strategy of pouring all my 2 litres of water into the container that is closest to overflowing. The example input would work out like this:
- I pour all my water into Container 2. It overflows, and I get one point.
- My friend pours all his water into Container 1. It overflows, and he gets one point.
- I pour all my water into Container 4. It now has 2L of water in it, so it needs another 2L to overflow.
- My friend pours all his water into Container 4. It overflows, and he gets one point.
- I pour all my water into container 3. It now has 2L of water in it, so it needs another 3L to overflow.
- My friend pours all his water into Container 3. It overflows, and he gets one point.
Altogether, I only got one point, and my friend got three. That's not good. I can do better than that if I make use of my ability to pass on my turn without pouring water:
- I pour all my water into Container 2. It overflows, and I get one point.
- My friend pours all his water into Container 1. It overflows, and he gets one point.
- I pass.
- My friend pours all his water into Container 4. It now has 3L of water in it, so it needs another 1L to overflow.
- I pour my water into Container 4. It overflows, and I get one point.
- My friend pours all his water into Container 3. It has 3L of water in it now, so it needs another 2L to overflow.
- I pour my water into Container 3. It overflows, and I get one point.
By strategically choosing which container to pour my water in, I have managed to obtain 3 points instead of 1. The output format is a list of space separated instructions, where P
indicates a pass, and an integer from 1
to n
indicates which container I poured water into. For instance, the naïve nonoptimal strategy listed above could be represented as:
2 4 3
Which means I poured into Container 2, then Container 4, then Container 3. The more optimal strategy would be represented as:
2 P 4 3
Which means I poured into Container 2, then passed, then Container 4, then Container 3 (This, by the way, is the output format).
Your program will take in input as described above and return the most optimal solution to get as many points as possible. If there is more than one optimal solution, output either.