H: Witchwood

https://open.kattis.com/problems/witchwood

Let CiC_i be the optimal expected time to build a house with ii logs. When building a house with i+1i+1 logs, we first need to place ii logs successfully, then place the last log; if the last log fails, then we must start the process over. The probability that the first ii logs fail does not matter, only the expected time it takes to place them; therefore, we should just choose the strategy with least expected time, i.e. the strategy that takes CiC_i time in expectation.

Suppose we choose log from location jj last and let CC be the overall expected time with this strategy (follow the optimal strategy for the first ii logs, then choose log jj as the i+1i+1th). We spend time Ci+TjC_i+T_j in expectation to collect the first ii logs and attempt log jj, and with probability PjP_j it fails and forces us to spend an additional K+CK+ C in expectation to redo the process. So

C=Ci+Tj+Pj(K+C)C = C_i + T_j + P_j(K + C)

and solving

C=11Pj(Ci+Tj+PjK)C = \frac{1}{1-P_j}(C_i + T_j + P_jK)

Computationally, we have enough time to check all possible jj to find the one which minimizes the expected time, so we can write a program that computes

Ci+1=minj11Pj(Ci+Tj+PjK)C_{i+1}= \min_j\frac{1}{1-P_j}(C_i + T_j + P_jK)

Last updated