H: Witchwood
https://open.kattis.com/problems/witchwood
Let Ci be the optimal expected time to build a house with i logs. When building a house with i+1 logs, we first need to place i logs successfully, then place the last log; if the last log fails, then we must start the process over. The probability that the first i 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 Ci time in expectation.
Suppose we choose log from location j last and let C be the overall expected time with this strategy (follow the optimal strategy for the first i logs, then choose log j as the i+1th). We spend time Ci+Tj in expectation to collect the first i logs and attempt log j, and with probability Pj it fails and forces us to spend an additional K+C in expectation to redo the process. So
C=Ci+Tj+Pj(K+C)
and solving
C=1−Pj1(Ci+Tj+PjK)
Computationally, we have enough time to check all possible j to find the one which minimizes the expected time, so we can write a program that computes
Ci+1=minj1−Pj1(Ci+Tj+PjK)
Last updated