跳到主要内容

東京大学 新領域創成科学研究科 メディカル情報生命専攻 2019年8月実施 問題7

Author

zephyr, 祭音Myyura

Description

Let A={a1,a2,,aN}A = \{a_1, a_2, \ldots, a_N\} (1a1,a2,,aNW,aiaj if ij,1NW)(1 \leq a_1, a_2, \ldots, a_N \leq W, a_i \neq a_j \text{ if } i \neq j, 1 \leq N \leq W) be an array of size NN, whose elements are different integers ranging from 11 to WW. Let S(A)S(A) be the number of all possible distinct arrays each of which can be created by iteratively applying the following operation to AA, 00 or more times.

Operation: choose arbitrary ii that satisfies 1iN11 \leq i \leq N - 1 and ai+ai+1<Wa_i + a_{i+1} < W, and swap the values of aia_i and ai+1a_{i+1}.

We denote the largest number in array AA by aha_h, and the smallest number by ala_l.

(1) Let A={4,1,10,3,2}A = \{4, 1, 10, 3, 2\}. Show S(A)S(A) for W=11,12W = 11, 12, respectively.

(2) When ah+al<Wa_h + a_l < W, prove S(A)=NS(Aal)S(A) = N \cdot S(A - a_l). Here, AalA - a_l denotes an array that is array AA with element ala_l removed.

(3) When ah+alWa_h + a_l \geq W, prove S(A)=S({a1,a2,,ah1})S({ah+1,ah+2,,aN})S(A) = S(\{a_1, a_2, \ldots, a_{h-1}\}) \cdot S(\{a_{h+1}, a_{h+2}, \ldots, a_N\}). Here, we define S()=1S(\emptyset) = 1.

(4) Show an algorithm that computes S(A)S(A) from AA using (2) and (3).

(5) Let W=NW = N. We define T(A)T(A) as the worst time complexity of the algorithm you showed in (4). Write T(A)T(A) in terms of NN, and prove it. You can assume that addition, comparison, copy of two elements take unit time.

(6) Let W=NW = N. Assuming that the N!N! possible permutations have equal probability of being generated as the input array, write the average time complexity of the algorithm you showed in (4).


A={a1,a2,,aN}A = \{a_1, a_2, \ldots, a_N\} (1a1,a2,,aNW,aiaj 当 ij,1NW)(1 \leq a_1, a_2, \ldots, a_N \leq W, a_i \neq a_j \text{ 当 } i \neq j, 1 \leq N \leq W) 为一个大小为 NN 的数组,其元素为 11WW 范围内的不同整数。设 S(A)S(A) 为可以通过对 AA 进行 00 次或多次以下操作创建的所有可能不同数组的数量。

操作:选择满足 1iN11 \leq i \leq N - 1ai+ai+1<Wa_i + a_{i+1} < W 的任意 ii,交换 aia_iai+1a_{i+1} 的值。

我们将数组 AA 中的最大数记为 aha_h,最小数记为 ala_l

(1) 设 A={4,1,10,3,2}A = \{4, 1, 10, 3, 2\}。分别显示 W=11,12W = 11, 12 时的 S(A)S(A)

(2) 当 ah+al<Wa_h + a_l < W 时,证明 S(A)=NS(Aal)S(A) = N \cdot S(A - a_l)。这里,AalA - a_l 表示从数组 AA 中移除元素 ala_l 后的数组。

(3) 当 ah+alWa_h + a_l \geq W 时,证明 S(A)=S({a1,a2,,ah1})S({ah+1,ah+2,,aN})S(A) = S(\{a_1, a_2, \ldots, a_{h-1}\}) \cdot S(\{a_{h+1}, a_{h+2}, \ldots, a_N\})。这里,我们定义 S()=1S(\emptyset) = 1

(4) 使用 (2) 和 (3) 展示一个从 AA 计算 S(A)S(A) 的算法。

(5) 设 W=NW = N。我们定义 T(A)T(A) 为你在 (4) 中展示的算法的最坏时间复杂度。用 NN 表示 T(A)T(A),并证明它。你可以假设两个元素的加法、比较、复制操作的时间为一个单位时间。

(6) 设 W=NW = N。假设 N!N! 个可能的排列作为输入数组被生成的概率相等,写出你在 (4) 中展示的算法的平均时间复杂度。

题目描述

A={a1,a2,,aN},A=\{a_1,a_2,\ldots,a_N\},

其中 1aiW1\le a_i\le W、各元素互异且 1NW1\le N\le W。允许对数组执行任意多次(含零次)操作:选择 1iN11\le i\le N-1,若

ai+ai+1<W,a_i+a_{i+1}<W,

则交换相邻元素 ai,ai+1a_i,a_{i+1}。记从 AA 可到达的不同数组总数为 S(A)S(A),数组最大、最小元素分别记为 ah,ala_h,a_lh,lh,l 同时表示其所在位置)。回答:

  1. A={4,1,10,3,2}A=\{4,1,10,3,2\},分别求 W=11W=11W=12W=12 时的 S(A)S(A)

  2. ah+al<Wa_h+a_l<W 时,证明

    S(A)=NS(Aal),S(A)=N\,S(A-a_l),

    其中 AalA-a_l 表示删除最小元素后保持其余相对顺序的数组。

  3. ah+alWa_h+a_l\ge W 时,证明

    S(A)=S({a1,,ah1})S({ah+1,,aN}),S(A)=S(\{a_1,\ldots,a_{h-1}\})\, S(\{a_{h+1},\ldots,a_N\}),

    并规定 S()=1S(\varnothing)=1

  4. 利用第 2、3 问的分解设计从 AA 计算 S(A)S(A) 的算法。

  5. W=NW=N,把第 4 问算法的最坏运行时间记为 T(A)T(A);假设两元素的加法、比较和复制均耗费单位时间,用 NN 表示该最坏复杂度并证明。

  6. 仍令 W=NW=N,若 N!N! 种输入排列等概率出现,求该算法的平均时间复杂度。

Kai

(1)

Given A={4,1,10,3,2}A = \{4, 1, 10, 3, 2\}:

For W=11W = 11

To find S(A)S(A) for W=11W = 11, we need to consider the operation condition ai+ai+1<Wa_i + a_{i+1} < W. We start with the initial array and systematically apply all valid swaps.

Initial array: {4,1,10,3,2}\{4, 1, 10, 3, 2\}

We need to check all possible adjacent swaps under the condition ai+ai+1<11a_i + a_{i+1} < 11:

Swap 44 and 11 (since 4+1=5<114 + 1 = 5 < 11):

{1,4,10,3,2} \{1, 4, 10, 3, 2\}

Swap 11 and 44 back (since 1+4=5<111 + 4 = 5 < 11):

{4,1,10,3,2} \{4, 1, 10, 3, 2\}

Swap 1010 and 33 (since 10+3=131110 + 3 = 13 \not< 11):

No swap \text{No swap}

Swap 33 and 22 (since 3+2=5<113 + 2 = 5 < 11):

{4,1,10,2,3} \{4, 1, 10, 2, 3\}

All valid permutations for W=11W = 11 are:

  1. {4,1,10,3,2}\{4, 1, 10, 3, 2\}
  2. {1,4,10,3,2}\{1, 4, 10, 3, 2\}
  3. {4,1,10,2,3}\{4, 1, 10, 2, 3\}
  4. {1,4,10,2,3}\{1, 4, 10, 2, 3\}

Therefore, for W=11W = 11:

S(A)=4S(A) = 4

For W=12W = 12

For W=12W = 12, we need to explore more possible swaps as the condition is more lenient.

Initial array: {4,1,10,3,2}\{4, 1, 10, 3, 2\}

We start with the initial array and apply all valid swaps:

Swap 44 and 11 (since 4+1=5<124 + 1 = 5 < 12):

{1,4,10,3,2} \{1, 4, 10, 3, 2\}

Swap 11 and 1010 (since 1+10=11<121 + 10 = 11 < 12):

{4,10,1,3,2} \{4, 10, 1, 3, 2\}

Swap 33 and 22 (since 3+2=5<123 + 2 = 5 < 12)

{4,1,10,2,3}\{4, 1, 10, 2, 3\}

Now explore permutations of {1,4,10,3,2}\{1, 4, 10, 3, 2\}:

Swap 33 and 22 (since 3+2=5<123+2=5<12):

{1,4,10,2,3}\{1, 4, 10, 2, 3\}

Now explore permutations of {4,10,1,3,2}\{4, 10, 1, 3, 2\}:

Swap 11 and 33 (since 1+3=4<121 + 3 = 4 < 12):

{4,10,3,1,2} \{4, 10, 3, 1, 2\}

Swap 33 and 22 (since 3+2=5<123 + 2 = 5 < 12):

{4,10,1,2,3} \{4, 10, 1, 2, 3\}

Now explore permutations of {4,10,3,1,2}\{4, 10, 3, 1, 2\}:

Swap 11 and 22 (since 1+2=3<121+2=3<12):

{4,10,3,2,1}\{4, 10, 3, 2, 1\}

Now explore permutations of {4,10,1,3,2}\{4, 10, 1, 3, 2\}:

Similarly, we get:

{4,10,2,3,1},{4,10,2,1,3}\{4, 10, 2, 3, 1\}, \{4, 10, 2, 1, 3\}

Thus, the valid permutations for W=12W = 12 are:

  1. {4,1,10,3,2}\{4, 1, 10, 3, 2\}
  2. {1,4,10,3,2}\{1, 4, 10, 3, 2\}
  3. {4,1,10,2,3}\{4, 1, 10, 2, 3\}
  4. {1,4,10,2,3}\{1, 4, 10, 2, 3\}
  5. {4,10,1,3,2}\{4, 10, 1, 3, 2\}
  6. {4,10,1,2,3}\{4, 10, 1, 2, 3\}
  7. {4,10,2,3,1}\{4, 10, 2, 3, 1\}
  8. {4,10,2,1,3}\{4, 10, 2, 1, 3\}
  9. {4,10,3,1,2}\{4, 10, 3, 1, 2\}
  10. {4,10,3,2,1}\{4, 10, 3, 2, 1\}

Therefore, for W=12W = 12:

S(A)=10S(A) = 10

(2)

When ah+al<Wa_h + a_l < W, prove S(A)=NS(Aal)S(A) = N \cdot S(A - a_l).

Given ah+al<Wa_h + a_l < W, the smallest number ala_l can be swapped with any adjacent element and can move to any position in the array.

Therefore, the number of permutations of AA is equal to the number of permutations of AalA - a_l, multiplied by the number of possible positions for ala_l, which is NN. Hence, we have:

S(A)=NS(Aal) S(A) = N \cdot S(A - a_l)

(3)

When ah+alWa_h + a_l \geq W, prove S(A)=S({a1,a2,,ah1})S({ah+1,ah+2,,aN})S(A) = S(\{a_1, a_2, \ldots, a_{h-1}\}) \cdot S(\{a_{h+1}, a_{h+2}, \ldots, a_N\}).

If ah+alWa_h + a_l \geq W, then ah+aiWa_h+a_i\geq W for every other element aia_i. Hence aha_h cannot be swapped and remains at position hh, dividing AA into two independent subarrays. Thus, the total number of distinct arrays is the product of the numbers for the two subarrays:

S(A)=S({a1,a2,,ah1})S({ah+1,ah+2,,aN}) S(A) = S(\{a_1, a_2, \ldots, a_{h-1}\}) \cdot S(\{a_{h+1}, a_{h+2}, \ldots, a_N\})

(4)

Algorithm to compute S(A)S(A):

def compute_S(A, W):
if len(A) == 0:
return 1

# O(N)
a_h = max(A)
a_l = min(A)

if a_h + a_l < W:
# O(N)
A_minus_a_l = [x for x in A if x != a_l]
return len(A) * compute_S(A_minus_a_l, W)
# T(N-1)
else:
index_h = A.index(a_h)
left_part = A[:index_h]
right_part = A[index_h+1:]
return compute_S(left_part, W) * compute_S(right_part, W)
# 2*T(N/2)

# Example
A = [4, 1, 10, 3, 2]
W = 11
print(compute_S(A, W)) # Output: 4
W = 12
print(compute_S(A, W)) # Output: 10

(5)

When W=NW=N, the NN distinct elements in {1,,N}\{1,\ldots,N\} form a permutation of that set. Thus ah=Na_h=N and al=1a_l=1, so ah+alWa_h+a_l\geq W and the algorithm always uses the split in (3). A call on NN elements takes Θ(N)\Theta(N) time to find the extrema and copy the subarrays. The worst case occurs when NN is at an end:

T(N)=T(N1)+Θ(N).T(N)=T(N-1)+\Theta(N).

Therefore:

T(N)=Θ ⁣(i=1Ni)=Θ(N2).\displaystyle T(N)=\Theta\!\left(\sum_{i=1}^N i\right)=\Theta(N^2).

(6)

In a uniformly random permutation, the position of the maximum is uniform on {1,,N}\{1,\ldots,N\}. Let Tˉ(N)\bar T(N) be the average running time. Then:

Tˉ(N)=1Nh=1N(Tˉ(h1)+Tˉ(Nh))+Θ(N)=2Nk=0N1Tˉ(k)+Θ(N).\bar T(N) =\frac1N\sum_{h=1}^{N}\bigl(\bar T(h-1)+\bar T(N-h)\bigr)+\Theta(N) =\frac2N\sum_{k=0}^{N-1}\bar T(k)+\Theta(N).

This is the standard randomized-quicksort recurrence and gives:

Tˉ(N)=Θ(NlogN).\bar T(N)=\Theta(N\log N).

Knowledge

组合计数 递归 复杂度分析 主定理

解题技巧和信息

  1. Recursive Reduction: Understand how reducing a problem by removing an element or splitting the array affects complexity.
  2. Complexity Analysis: Sum of first NN numbers is N(N+1)2\frac{N(N+1)}{2}.
  3. Master Theorem: Useful for solving recurrence relations in divide-and-conquer algorithms.

重点词汇

  • worst-case 最坏情况
  • average-case 平均情况
  • time complexity 时间复杂度
  • recurrence relation 递推关系

参考资料

  1. Introduction to Algorithms by Cormen, Leiserson, Rivest, and Stein, Chapter 4.