To know about expected value, you can read this.
Subtask 1:
At first, let’s sort the array of the mugs’ capacities non-decreasingly. Now, if we give the mug to a customer, for the future customers, we can only use the valid mugs in range. The probability of choosing a mug for the customer is obviously . Now, let’s say the jug’s present water amount is liters and we’ve chosen to give the mug to a customer. So, the expected number of times you can serve the customers from this state to the final states is:
,
where , is the leftmost index such that and is the rightmost index such that . If is the final state, .
From the above relation, we can easily build a dynamic programming solution.
Author’s solution for subtask 1: https://ideone.com/9eZgDh
Complexity:
Subtask 2:
For subtask 2, we just need to optimize the idea of subtask 1. From the relation above, we can see that from a state , we can go to the next states only with a fixed water amount of liters. If we know the sum of the expected values and the count of the next valid states with liters water, we can easily calculate . Let, is our desired count and is our desired sum of the expected values with liters water. Then we can write,
and can be calculated by iterating from the index to the index. We need to process the mugs with the same capacities together so that the mugs with the same capacities won’t be considered as the next states. See the author’s solution for better understandings.
Author’s solution: https://ideone.com/Nog7Jm
Complexity: