跳到主要内容

東京大学 情報理工学系研究科 創造情報学専攻 2014年8月実施 筆記試験 第1問

Author​

itsuitsuki

Description​

Official examination, archived Japanese PDF. Let a stack of nn pancakes with different sizes be given. A spatula is a tool to flip over pancakes. If you put the spatula under the kk-th pancake from the top, all top to the kk-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 kk-th smallest in advance. From now, we use this pancake-number kk 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 n=3n=3, 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 n=2n=2.

(2) For n=3n=3, 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 n=4n=4, 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 nn, describe an algorithm for rearrangement to reach "ordered-state" and give its time complexity.

题目描述​

给定 nn 张大小互异的煎饼叠成的一摞。把锅铲插到从顶端数第 kk 张下方,可把最上面的 kk 张整体翻转并逆序放回。目标是使最小煎饼在最上方、大小向下单调增加、最大煎饼在最下方,称为“有序状态”。假设煎饼两面相同,且预先知道每张煎饼是第 kk 小,直接以该编号标识;“堆叠状态”用从上到下的编号序列表示。翻转示例见原文图 1、2。

  1. 对 n=3n=3,画状态转移图:顶点为所有堆叠状态,弧表示一次锅铲翻转。图 3 给出 n=2n=2 的示例。
  2. 对 n=3n=3,给出一个到有序状态所需最少翻转次数达到最大值的初始状态,并给出该次数。
  3. 对 n=4n=4 完成同样任务。
  4. 对一般 nn,说明一种把任意状态变为有序状态的算法,并给出其时间复杂度。

Kai​

Let FkF_k reverse the first kk entries. Each flip is its own inverse. The ordered state is 12⋯n12\cdots n, because the numbering increases with size. As in the original n=2n=2 graph, omit the unhelpful k=1k=1 self-loops.

(1) State graph for n=3n=3​

There are 3!=63!=6 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 n=3n=3​

The opposite vertex 132132 is three edges from 123123:

132→F2312→F3213→F2123.\boxed{132\xrightarrow{F_2}312\xrightarrow{F_3}213\xrightarrow{F_2}123.}

No vertex on the six-cycle is farther away, and no path from 132132 to 123123 uses fewer than three edges. The maximum required minimum number of flips is therefore 3\boxed{3}.

(3) Maximum distance for n=4n=4​

The maximum minimum number is 4\boxed{4}, attained by 42314231, 24132413 and 31423142. For example,

4231→F41324→F23124→F32134→F21234.\boxed{4231\xrightarrow{F_4}1324\xrightarrow{F_2}3124 \xrightarrow{F_3}2134\xrightarrow{F_2}1234.}

To verify minimality and the global maximum, breadth-first search from 12341234 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 flipsStates
01234
12134, 3214, 4321
23124, 4312, 2314, 4123, 3421, 2341
31324, 4213, 3412, 1342, 4132, 1423, 2143, 2431, 1243, 3241, 1432
44231, 2413, 3142

(4) General sorting algorithm​

For m=n,n−1,…,2m=n,n-1,\ldots,2, locate pancake mm in the first mm positions. If it is already at position mm, 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 mm pancakes to put it at position mm. Later flips use only smaller prefixes, so this correct suffix is preserved. Induction on decreasing mm 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 1,…,n1,\ldots,n. Locating and reversing prefixes of length at most mm costs O(m)O(m) in an array, so total computational time is O(n2)\boxed{O(n^2)}. The number of spatula operations is at most 2n−32n-3 for n≥2n\ge2 (at most two for each m≥3m\ge3 and one for m=2m=2). 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.