编程介的小学生 2017-03-28 14:26 采纳率: 20.5%
浏览 705
已采纳

Railroad Sort

Consider the railroad station that has n dead-ends designed in a way shown on the picture. Dead-ends are numbered from right to left, starting from 1.

Let 2n railroad cars get from the right. Each car is marked with some integer number ranging from 1 to 2n, different cars are marked with different numbers.

You can move the cars through the dead-ends using the following two operations. If the car x is the first car on the path to the right of the dead-end i, you may move this car to this dead-end. If the car y is the topmost car in the dead-end j you can move it to the path on the left of the dead-end. Note, that cars cannot be moved to the dead-end from the path to its left and cannot be moved to the path on the right of the dead-end they are in.

Your task is to rearrange the cars so that the numbers on the cars listed from left to right were in the ascending order and all the cars are to the left of all the dead-ends.

One can prove that the required rearranging is always possible.

Input

The input contains multiple test cases. Each test case occupies two lines. The first line of each case contains n - the number of dead-ends (1 <= n <= 13). The second line contains 2n integer numbers - the numbers on the cars, listed from left to right.

A case with n = 0 ends up the input file.

Output

For each case, output the sequence of operations in one line. Each operation is identified with the number of the car moved in this operation. The type of the operation and the dead-end used are clearly determined uniquely.

Sample Input

2
3 2 1 4
2
1 2 3 4
0

Sample Output

3 3 2 2 1 1 4 4 3 2 1 1 2 3 4 4
1 2 2 1 2 1 3 3 4 4 1 2 3 3 4 4

  • 写回答

2条回答 默认 最新

  • threenewbee 2017-04-06 15:50
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码