Simple Probability, Dynamic Programming
Two players play a game on the array of numbers. They make moves alternatively. At each move player can take first or the last number with equal probability. The taken number then removed. Find the expected sum of numbers taken by the first player during the whole game.
Most of you have found O(N * N) DP solution for this problem. But such solution is too slow since it is not able to find answer for up to 500 tests quickly. To get the fast enough solution one should notice that the answer has the form C[N] * V + … + C[N][N] * V[N], where numbers C[N][K] can be found by similar DP as in standard solution. So we get a solution with complexity O(N * N + T * N): O(N * N) for calculating C[N][K] before processing tests and then O(N) for each test.
Denote by V, …, V[N] our numbers. Denote by G(i, j) the game described above but played only with numbers V, V[i + 1], …, V[j]. Further, denote by E[j] the expected sum of numbers taken by the first player during the game G(i, j) and by E2[j]* the same number but for the second player. Clearly the answer is E[N].
We can use dynamic programming to calculate E[j]. At first let i = j. Then the first player takes the only available number V, so E = V***.
Now let’s i < j. If the first player takes the first number, that is, the number V*, then we arrive at the game G(i + 1, j) where he is now the second player, so his expected sum is E2[i + 1][j]. Since this happens with probability 1 / 2 this case will add (V + E2[i + 1][j]) / 2* to the expected sum E[j]. Similarly if he takes the last number V[j] we should add (V[j] + E2[j − 1]) / 2 to the E[j]*. So we get the formula**E*[j] = (V* + E2[i + 1][j]) / 2 + (V[j] + E2*[j − 1]) / 2**.
Let’s get rid of the function E2 in it. For this we note that**E2*[j] = V* + V[i + 1] + ... + V[j] − E*[j]**.
Substituting such expressions but for E2[i + 1][j] and E2[j − 1]* in the formula for E[j]* we get after straightforward calculations**E*[j] = V* + ... + V[j] − (E[i + 1][j] + E*[j − 1]) / 2**.
Precomputing sums S = V + … + V** and noting that V + … +V[j] = S[j] − S[i − 1]* we arrive at O(N * N) solution for this problem mentioned above which appears to be slow.
To get the fast enough solution let’s analyze this DP solution more carefully. As was mentioned E = V***. Next we consider E[i + 1]*. By the main formula we have**E*[i + 1] = V* + V[i + 1] − (E[i + 1][i + 1] + E**) / 2 = V* + V[i + 1] − (V[i + 1] + V*) / 2 = V* / 2 + V[i + 1] / 2**.
So E[i + 1] = V / 2 + V[i + 1] / 2**. Now let’s consider E[i + 2]. Again by the main formula and by the formula for E[i + 1] obtained just now, which can be used for E[i + 1][i + 2] as well, we get**E*[i + 2] = V* + V[i + 1] + V[i + 2] − (E[i + 1][i + 2] + E*[i + 1]) / 2 = V* + V[i + 1] + V[i + 2] − (V[i + 1] / 2 + V[i + 2] / 2 + V* / 2 + V[i + 1] / 2) / 2 = V* * 3 / 4 + V[i + 1] / 2 + V[i + 2] * 3 / 4**.
So E[i + 2] = V * 3 / 4 + V[i + 1] / 2 + V[i + 2] * 3 / 4**.
We can guess from here that**E*[j] = C[L] * V* + ... + C[L][L] * V[j]**,
where L = j − i + 1 is the length of the current game segment. For example,
C = 1,
C = 1 / 2, C = 1 / 2,
C = 3 / 4, C = 1 / 2, C = 3 / 4.
We can get the recurrent formulas for numbers C[L][k] substituting the formulas for E[j], E[i + 1][j] and E[j + 1] in terms of C[L][k] in the main recurrent formula for E[j]. Watching over the coefficients at each of the numbers V, …, V[j] we arrive at the following formula for C[L][k]:**C[L][k] = 1 − (C[L − 1][k − 1] + C[L − 1][k]) / 2**, for **k** from 1 to **L**,
Where we set for convenience C[L] = C[L][L + 1] = 0.
So the solution can be now implemented as follows. At first before reading any data compute numbers C[L][k] for L from 1 to 2000 and for k from 1 to L by the recurrent formulas in a simple double loop. Then for each test read the corresponding sequence of numbers and find the answer by the formula mentioned in the QUICK EXPLANATION. We can even calculate the answer on the fly without saving the sequence of numbers. See setter’s and tester’s solutions for details.
Finally, note that C[L][k] is the probability that first player will take the k-th diamond in the game with L diamonds numbered as 1, 2, …, L. Thanks to pragrame for mentioning that. Also see the corresponding comment in author’s solution.
The problem can be solved without exploiting the relation between E[j]* and E2[j]. Instead one should obtain similar recurrence for E2[j]. And then both these values can be expressed as linear combinations of numbers V, …, V[j]* with some coefficients, for which some similar recurrent formulas as for our coefficients C[L][k] can be obtained. But such solution requires twice more memory. Also it is quite long to describe it in the same details as the previous one. Hence see as a reference the following solution by ballon_ziq.
AUTHOR’S AND TESTER’S SOLUTIONS:
Game - 2 (In Russian only)