跳到正文

个人记录

hanoisort

Milmon约 9 分钟
原文

这是 2026年1月23日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年1月23日
更新
2026年6月6日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

First of all, consider reversing the whole process: we have 1∼n1\sim n in order on pillar BB at the beginning, and we need to move them to AA arranged in the given order pp.

Now consider p1p_1. We can see that:

  • at some moment, p1p_1 is moved from the top of BB or CC to the bottom of AA.
  • before this move, AA must be empty, and BB and CC are complementary subsequences of 1∼n1\sim n.
  • before this move, p1p_1 must be located at the top of one of BB and CC, and all disks <p1<p_1 must be located at the other pillar.
  • after this move, p1p_1 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 p1p_1 and continue processing p2p_2, then p3p_3, 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 BB to CC or from CC to BB) be xx.
  • move 1∼(x−1)1\sim(x-1) to AA, one by one.
  • move xx to the opposite pillar.
  • move (x−1)∼1(x-1)\sim 1 one by one to BB or CC as you wish.
  • the number of moves is 2x−12x-1 (assuming 1∼x1\sim x are all still present).

Now let’s go back and consider p1p_1. At the moment the best solution is to operate x=p1−1x=p_1-1 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 x=tx=t, all previous operations with x<tx<t 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 xx 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, {pos,optpos,diff}\{pos,optpos,diff\}:

  • pospos is the position of the referred disk from bottom to top in all present disks.
  • optposoptpos 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 optposoptpos.
  • diff∈{true,false}diff\in\{true,false\} means whether the disk at pospos and pos+1pos+1 are located on different pillars (i.e. whether one of them is located at BB and the other one is located at CC).

Now I’ll describe the specific process of the algorithm. Assuming we’re in the ii-th operation, where p1,p2,…,pi−1p_1,p_2,\ldots,p_{i-1} have all been put in place, and we need to put pip_i in place. Therefore, let the number of present disks be m=n−i+1m=n-i+1. Let pospos be the position of pip_i from bottom to top (this can be calculated with a simple fenwick tree). Initialize optpos:=∞optpos:=\infty and cost:=0cost:=0.

  • pop the top node ndnd that satisfies nd.pos>posnd.pos>pos one by one. For each popped ndnd:

    • if nd.diff=truend.diff=true, 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 nd.posnd.pos, with cost:=cost+2×(min⁡{optpos,nd.optpos}−nd.pos)cost:=cost+2\times(\min\{optpos,nd.optpos\}-nd.pos); or reinitiate an operation, with cost:=max⁡{0,2×(m−nd.pos)−1}cost:=\max\{0,2\times(m-nd.pos)-1\}. We pick the one with smaller cost. Finally, let optpos:=nd.posoptpos:=nd.pos.
    • if nd.diff=falsend.diff=false, let optpos:=min⁡{optpos,nd.optpos}optpos:=\min\{optpos,nd.optpos\}.
  • after stack popping, if the top node has nd.pos=posnd.pos=pos, we need to handle this node specially, since now we need to make the disks at pospos and pos+1pos+1 located at different pillars instead of the same pillar.

    • if nd.diff=truend.diff=true, let optpos:=min⁡{optpos,nd.optpos}optpos:=\min\{optpos,nd.optpos\}.
    • if nd.diff=falsend.diff=false, let cost:=cost+2×(min⁡{optpos,nd.optpos}−nd.pos)cost:=cost+2\times(\min\{optpos,nd.optpos\}-nd.pos) or cost:=max⁡{0,2×(m−nd.pos)−1}cost:=\max\{0,2\times(m-nd.pos)-1\}, and optpos:=nd.posoptpos:=nd.pos.
    • this node is also popped.
  • if the top node has nd.pos<posnd.pos<pos, or that the stack is empty, we still need to do something to make the disks at pospos and pos+1pos+1 located at different pillars. Let cost:=min⁡{cost+2×(optpos−pos),max⁡{0,2×(i−pos)−1}}cost:=\min\left\{cost+2\times(optpos-pos),\max\{0,2\times(i-pos)-1\}\right\} and optpos:=posoptpos:=pos.

  • after all above procedures, finally we need to push a node {pos−1,optpos,true}\{pos-1,optpos,true\} onto the stack. However, if there is already a node with nd.pos=pos−1nd.pos=pos-1 at the top of the stack, then we need to do nd.optpos:=min⁡{nd.optpos,optpos}nd.optpos:=\min\{nd.optpos,optpos\} and nd.diff:=¬nd.diffnd.diff:=\neg nd.diff.

The answer is n+∑icostn+\sum_i cost.