Editorial for WC '18 Contest 1 S3 - Reach for the Top


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

This problem may be solved with two distinct approaches.

Graph Theory Approach

Let's represent the rope as a directed graph with 2H nodes (numbered from 0 to 2H-1), with each node h representing Bob holding onto the rope at a height of h. Each action which would take Bob from a height h_1 to another height h_2 then corresponds to an edge from node h_1 to node h_2. We're then looking for the shortest distance (minimum sum of edge weights) from node 0 to any node from H upwards.

BFS (Breadth First Search) is a well-known algorithm which can be used to compute shortest distances on unweighted graphs such as this one. The time complexity of BFS is \mathcal O(V+E), where V is the number of nodes in the graph, and E is the number of edges. In this case V is in \mathcal O(H), but E is in \mathcal O(H^2), as each node has \mathcal O(H) outgoing edges corresponding to possible drop actions from it. This gives us an overall time complexity of \mathcal O(H^2+N), which is too slow to earn full marks.

It's possible to optimize this approach by making the graph weighted, and adding an extra set of 2H nodes to it. Let's refer to the original nodes as X_{0 \dots (2H-1)}, and the new nodes as Y_{0 \dots (2H-1)}, with each new node Y_h representing Bob being mid-drop at a height of h. We'll want to have a weight-0 edge from each node Y_h to Y_{h-1}, representing the continuation of a drop. The start of a drop can then be represented by a weight-1 edge from each node X_h to Y_h, while the end of a drop can be represented by a weight-0 edge from each node Y_h back to X_h. This removes the need for \mathcal O(H) outgoing drop-related edges from each node!

We can no longer use BFS due to the graph being weighted, but in this case, we can use a variant of it which works when all edge weights are equal to either 0 or 1. This variant uses a deque rather than a queue, and pushes nodes coming from weight-0 edges onto the front rather than the back of the deque. It similarly runs in \mathcal O(V+E). Since V and E are now in \mathcal O(H), this gives us an overall time complexity of \mathcal O(H+N), which is fast enough. Note that Dijkstra's algorithm could be used instead, though it's slower and slightly more complex.

Dynamic Programming Approach

Let DP[h] be the minimum number of jumps required for Bob to hold onto the rope at a height of h, and let M[d] be the maximum height h such that DP[h] = d. Initially, we have DP[0] = 0 and M[0] = 0, and we'll let M[-1] = -1 for convenience. We'll proceed to fill in the DP values in a series of phases corresponding to increasing d values, starting with d = 0. For each d, we'll iterate over heights h from M[d-1]+1 up to M[d]. For each such h, either DP[h] = d, or DP[h] has yet to be filled in and we can set it to d+1 (due to dropping down from a height of M[d]). Either way, we'll end up with this phase of DP values all filled in, and from each h we can fill in DP[h+J] as well, while updating M[DP[h+J]] as appropriate. As soon as we arrive at M[d] \ge H, d must be the answer, and if we instead run out of new phases to explore, then the answer must be -1.


Comments

There are no comments at the moment.