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 ithi^{th} mug to a customer, for the future customers, we can only use the valid mugs in [i+1,n][i+1, n] range. The probability of choosing a mug ii for the 1st1^{st} customer is obviously 1n\frac{1}{n}. Now, let’s say the jug’s present water amount is cc liters and we’ve chosen to give the ithi^{th} mug to a customer. So, the expected number of times you can serve the customers from this state to the final states is:

E(i,c)=1+1(rl+1)×(E(l,cai)+E(l+1,cai)++E(r,cai))E(i,c) = 1 + \frac{1}{(r-l+1)} \times (E(l, c-a_i) + E(l+1, c-a_i) + … + E(r, c-a_i)),

where i<lri< l\leq r, ll is the leftmost index such that ai<alcaia_i < a_l\leq c-a_i and rr is the rightmost index such that ai<arcaia_i < a_r \leq c-a_i. If E(i,c)E(i,c) is the final state, E(i,c)=1E(i,c) = 1.

From the above relation, we can easily build a dynamic programming solution.
Author’s solution for subtask 1: https://ideone.com/9eZgDh
Complexity: O(n2×c)O(n^2 \times c)

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 E(i,c)E(i,c), we can go to the next states only with a fixed water amount of caic-a_i liters. If we know the sum of the expected values and the count of the next valid states with caic-a_i liters water, we can easily calculate E(i,c)E(i, c). Let, cntkcnt_k is our desired count and sumksum_k is our desired sum of the expected values with kk liters water. Then we can write,

E(i,c)=1+1cntcai×sumcaiE(i,c) = 1 + \frac{1}{cnt_{c-a_i}} \times sum_{c-a_i}

cntkcnt_k and sumksum_k can be calculated by iterating from the nthn^{th} index to the 1st1^{st} 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: O(n×c)O(n\times c)

Statistics

25% Solution Ratio
mdarikrayhanEarliest, Apr '23
mdarikrayhanFastest, 0.1s
nusuBotLightest, 4.9 MB
mdarikrayhanShortest, 1087B
Toph uses cookies. By continuing you agree to our Cookie Policy.