東京大学 情報理工学系研究科 創造情報学専攻 2014年8月実施 筆記試験 第1問
Author
Description
Official examination, archived Japanese PDF. Let a stack of pancakes with different sizes be given. A spatula is a tool to flip over pancakes. If you put the spatula under the -th pancake from the top, all top to the -th pancakes are flipped over and placed in the reverse order (Fig.1). Let us rearrange the stack using a spatula so that the smallest pancake appears on the top of the stack, monotonically increasing the size, and the largest at the bottom, which we call "ordered-state". We assume that both sides of each pancake are identical and we know which pancake is the -th smallest in advance. From now, we use this pancake-number to identify the pancake. A "stack-state" is denoted by the sequence of pancake-numbers from the top to the bottom. For example, using our notation, state transitions in Fig. 1 are described as in Fig. 2. Answer the following questions.
(1) For , draw a state transition graph, whose vertices are "stack-states" and arcs are transitions by a spatula. Fig. 3 shows one example of the state transition graph for .
(2) For , give an example of "stack-state" which requires the maximum number of flips to reach "ordered-state", and the corresponding number of flips.
(3) For , give an example of "stack-state" which requires the maximum number of flips to reach "ordered-state", and the corresponding number of flips.
(4) For general , describe an algorithm for rearrangement to reach "ordered-state" and give its time complexity.
题目描述
给定 张大小互异的煎饼叠成的一摞。把锅铲插到从顶端数第 张下方,可把最上面的 张整体翻转并逆序放回。目标是使最小煎饼在最上方、大小向下单调增加、最大煎饼在最下方,称为“有序状态”。假设煎饼两面相同,且预先知道每张煎饼是第 小,直接以该编号标识;“堆叠状态”用从上到下的编号序列表示。翻转示例见原文图 1、2。
- 对 ,画状态转移图:顶点为所有堆叠状态,弧表示一次锅铲翻转。图 3 给出 的示例。
- 对 ,给出一个到有序状态所需最少翻转次数达到最大值的初始状态,并给出该次数。
- 对 完成同样任务。
- 对一般 ,说明一种把任意状态变为有序状态的算法,并给出其时间复杂度。
Kai
Let reverse the first entries. Each flip is its own inverse. The ordered state is , because the numbering increases with size. As in the original graph, omit the unhelpful self-loops.
(1) State graph for
There are states. The graph is a six-cycle; each displayed bidirectional edge represents both directed transitions and is labelled by the flip length:
(2) Maximum distance for
The opposite vertex is three edges from :
No vertex on the six-cycle is farther away, and no path from to uses fewer than three edges. The maximum required minimum number of flips is therefore .
(3) Maximum distance for
The maximum minimum number is , attained by , and . For example,
To verify minimality and the global maximum, breadth-first search from gives the following complete partition of all 24 states. Every new state is reached by one flip from the preceding level, and no earlier level contains it.
| Minimum flips | States |
|---|---|
| 0 | 1234 |
| 1 | 2134, 3214, 4321 |
| 2 | 3124, 4312, 2314, 4123, 3421, 2341 |
| 3 | 1324, 4213, 3412, 1342, 4132, 1423, 2143, 2431, 1243, 3241, 1432 |
| 4 | 4231, 2413, 3142 |
(4) General sorting algorithm
For , locate pancake in the first positions. If it is already at position , do nothing. Otherwise, if it is not at the top, flip the prefix ending at its position to bring it to the top; then flip the first pancakes to put it at position . Later flips use only smaller prefixes, so this correct suffix is preserved. Induction on decreasing proves that the final state is ordered.
def pancake_sort(values):
a = list(values)
flips = []
for m in range(len(a), 1, -1):
k = a.index(m, 0, m) + 1
if k == m:
continue
if k > 1:
a[:k] = a[:k][::-1]
flips.append(k)
a[:m] = a[:m][::-1]
flips.append(m)
return a, flips
The input is a permutation of . Locating and reversing prefixes of length at most costs in an array, so total computational time is . The number of spatula operations is at most for (at most two for each and one for ). Thus the number of physical flips is linear even though this straightforward array implementation takes quadratic time. It is a sorting algorithm, not a claim to find a shortest flip sequence for every input.