First of all, consider reversing the whole process: we have in order on pillar at the beginning, and we need to move them to arranged in the given order .
Now consider . We can see that:
- at some moment, is moved from the top of or to the bottom of .
- before this move, must be empty, and and are complementary subsequences of .
- before this move, must be located at the top of one of and , and all disks must be located at the other pillar.
- after this move, is put in place and no longer operated as there’s no need to do so, otherwise the solution won’t be optimal.
Then we can ignore and continue processing , then , and so on, following the same logic.
Therefore, we only care about the states, i.e. the complementary subsequences in each iteration. In order to transit between states, we can see that the feasible operational methods are actually very restricted. The only optimal type of operation is as follows:
- let the largest disk that needs to be changed to the opposite pillar (from to or from to ) be .
- move to , one by one.
- move to the opposite pillar.
- move one by one to or as you wish.
- the number of moves is (assuming are all still present).
Now let’s go back and consider . At the moment the best solution is to operate disks, but this may not be the desired optimal state as there is potential to facilitate future iterations by operating more disks.
We can record the best operations for now, and extend them afterwards when we find it possible to optimize and facilitate the current iteration.
Whenever we make an operation with , all previous operations with will no longer be useful. Therefore, the records can be maintained with a monotonic stack, where the nodes closer to the top of the stack has smaller and more recent operation times. Additionally, we need to keep track of non-trivial disks which are located at the opposite pillar to the disk above them.
Specifically, let the node struct consist of 3 variables, :
- is the position of the referred disk from bottom to top in all present disks.
- is the position (from bottom to top) reached by the corresponding operation, related to the referred disk. In other words, the corresponding operation affected all disks at and above .
- means whether the disk at and are located on different pillars (i.e. whether one of them is located at and the other one is located at ).
Now I’ll describe the specific process of the algorithm. Assuming we’re in the -th operation, where have all been put in place, and we need to put in place. Therefore, let the number of present disks be . Let be the position of from bottom to top (this can be calculated with a simple fenwick tree). Initialize and .
-
pop the top node that satisfies one by one. For each popped :
- if , i.e. the disk above the referred disk is located at the opposite pillar, we need to make them located at the same pillar. We either extend an operation to , with ; or reinitiate an operation, with . We pick the one with smaller cost. Finally, let .
- if , let .
-
after stack popping, if the top node has , we need to handle this node specially, since now we need to make the disks at and located at different pillars instead of the same pillar.
- if , let .
- if , let or , and .
- this node is also popped.
-
if the top node has , or that the stack is empty, we still need to do something to make the disks at and located at different pillars. Let and .
-
after all above procedures, finally we need to push a node onto the stack. However, if there is already a node with at the top of the stack, then we need to do and .
The answer is .