I: City Destruction
https://open.kattis.com/problems/city
Let dp[i][0] denote the (minimum) number of moves required to destroy all the cities from i through N, assuming that the (i−1)thcity (if exists) was not destroyed before the ith city.
Let dp[i][1] denote the (minimum) number of moves required to destroy all the cities from i through N, assuming that the (i−1)thcity (if exists) was destroyed before the ith city.
For convenience, set e0=0.
Initial condition:
dp[n][0]=⌈dhn⌉, dp[n][1]=⌈dmax(hn−en−1,0)⌉
For i<n, it is easy to see that the transitions are
dp[i][0]=min(dp[i+1][0]+⌈d(max(hi−ei+1,0)⌉,dp[i+1][1]+⌈dhi⌉))
If we assume city i+1was destroyed earlier, then our health reduces by ei+1and the number of extra moves required is ⌈d(max(hi−ei+1,0)⌉.
Otherwise, the number of extra moves required is ⌈dhi⌉.
dp[i][1]=min(dp[i+1][0]+⌈d(max(hi−ei+1−ei−1,0)⌉,dp[i+1][1]+⌈dmax(hi−ei−1,0)⌉))
In this case, since city i−1 has exploded already, our health has already reduced to max(hi−ei−1,0).
If we assume city i+1was destroyed earlier, then our health reduces by ei+1and the number of extra moves required is ⌈d(max(hi−ei+1−ei−1,0)⌉.
Otherwise, the number of extra moves required is ⌈dmax(hi−ei−1,0)⌉.
The final answer is dp[1][0].
Remember to use long integers to avoid overflow!
Last updated