E - Yacht Editorial by en_translator
This problem can be solved with a DP (Dynamic Programming), or memorized recursion.
Let \(f(k,S)\) be the maximum expected value when you can roll dices \(k\) more times, and the multiset of the numbers shown on the kept dice is \(S\).
Considering the multiset of dice note kept and its sub-multiset \(T\) of dice to be kept next, we can compute \(f(k,S)\) as
\(f(k,S)=\begin{cases} \sum_P \mathrm{prob}(P)\max_{T\subset P}f(k-1,S\cup T) & \text{if } k>1\\ \sum_P \mathrm{prob}(P)\mathrm{Score}(S\cup P)& \text{if } k=1, \end{cases}\)
where \(\mathrm{Prob}(P)\) is the probability that the multiset of numbers shown by a dice roll, and \(\mathrm{Score}(S)\) is the score if the game ends when the numbers shown by the dice is \(\mathrm{Score}(S)\).
The sought value is \(f(3,\emptyset)\).
\(f(k,S)\) calls \(f\) as many times as the pairs of \((P,T)\), which is at most \(6^{5-|S|}\times 2^{5-|S|}\), so the number of times \(f\) is recursive called when evaluating \(f(3,\emptyset)\) using memorized recursion is bounded by \(2\sum_S 12^{5-|S|}\leq 10^6\).
With an appropriate precalculation, we may assume that \(\mathrm{prob}\) and \(\mathrm{Score}\) may be evaluated in \(O(1)\) time, the memorized recursion allows us to solve the problem fast enough.
In the sample code below, the possible values for \(P\) and \(T\) are enumerated not as multisets but as sequences, and there are \(6^{5-|S|}\) and \(2^{5-|S|}\) such candidates. Also, we do not precalculate \(\mathrm{Score}\), and perform all the computation using a rational number class. Nevertheless, the code runs sufficiently fast within the execution time limit.
posted:
last update: