東京大学 情報理工学系研究科 コンピュータ科学専攻 2022年8月実施 専門科目 問題2
Author
zephyr, 祭音Myyura
Description
We consider a division of a set of mutually distinct n positive integers P={x1,x2,…,xn} into m sets P1,P2,…,Pm (1<m<n,P=P1∪P2∪…∪Pm,∀i,j(i=j)Pi∩Pj=∅), where ∅ denotes an empty set. The set sequence Q=[P1,P2,…,Pm] is called a division of P. We denote by ∥S∥ the summation of all the integers in S if S is a set of integers or a stack consisting of integers. Note that ∥S∥=0 in case S is an empty set or an empty stack.
Let maxsum(Q)=maxi∥Pi∥ for a division Q=[P1,P2,…,Pm] of P. Let minmaxsum(P,m) denote the minimum value of maxsum(Q) among all the possible divisions Q of P into m sets.
The following pseudo code shows an algorithm that computes an approximation of minmaxsum(P,m). Below, push(S,x) pushes x onto the stack S, pop(S) pops the top element of the stack S and returns the popped element, and top(S) returns the top element of the stack S. Note that top(S) and pop(S) return the same value for the same stack S, but top(S) does not modify the stack.
approx-minmaxsum(integer set P,integer m)1:Q=[P1,P2,…,Pm]←An arbitrary division of P into m sets;2:for (1≤i≤m)3:Si←an empty stack;4:foreach (x∈Pi) push(Si,x);5:while(1)6:j←argmaxi∥Si∥;/* j←one of the i’s that maximize ∥Si∥ */;7:k←argmini∥Si∥;/* k←one of the i’s that minimize ∥Si∥ */;8:if (top(Sj)+∥Sk∥≥∥Sj∥) break;9:push(Sk,pop(Sj));10:return ∥Sj∥;
Answer the following questions.
(1) Calculate minmaxsum({3,4,5,6},2).
(2) Show minmaxsum(P,m)≥∥P∥/m.
(3) Show that approx-minmaxsum(P,m)≤2⋅minmaxsum(P,m) holds, regardless of whatever division Q is chosen in line 2 of the above code.
(4) Show that the while loop in the above code will be repeated at most n times, regardless of whatever division Q is chosen in line 2.
(5) Describe data structures needed to make the above algorithm run in O(nlogm) time, and explain how to use them.
以下是中文翻译:
我们考虑将一组相互不同的 n 个正整数 P={x1,x2,…,xn} 划分为 m 个集合 P1,P2,…,Pm (1<m<n,P=P1∪P2∪…∪Pm,∀i,j(i=j)Pi∩Pj=∅),其中 ∅ 表示空集。集合序列 Q=[P1,P2,…,Pm] 称为 P 的划分。我们用 ∥S∥ 表示 S 中所有整数的和,如果 S 是一个整数集合或由整数组成的堆栈。注意,若 S 是空集合或空堆栈,则 ∥S∥=0。
设 maxsum(Q)=maxi∥Pi∥ 为 P 的划分 Q=[P1,P2,…,Pm]。设 minmaxsum(P,m) 为在 P 所有可能划分 Q 中 maxsum(Q) 的最小值。
下面的伪代码展示了一种计算 minmaxsum(P,m) 近似值的算法。下面,push(S,x) 将 x 压入堆栈 S,pop(S) 弹出堆栈 S 的顶部元素并返回弹出的元素,而 top(S) 返回堆栈 S 的顶部元素。注意,top(S) 和 pop(S) 为相同堆栈 S 返回相同的值,但 top(S) 不会修改堆栈。
approx-minmaxsum(integer set P,integer m)1:Q=[P1,P2,…,Pm]←An arbitrary division of P into m sets;2:for (1≤i≤m)3:Si←an empty stack;4:foreach (x∈Pi) push(Si,x);5:while(1)6:j←argmaxi∥Si∥;/* j←one of the i’s that maximize ∥Si∥ */;7:k←argmini∥Si∥;/* k←one of the i’s that minimize ∥Si∥ */;8:if (top(Sj)+∥Sk∥≥∥Sj∥) break;9:push(Sk,pop(Sj));10:return ∥Sj∥;
回答以下问题。
(1) 计算 minmaxsum({3,4,5,6},2)。
(2) 证明 minmaxsum(P,m)≥∥P∥/m。
(3) 证明不论第 2 行选择何种划分 Q,approx-minmaxsum(P,m)≤2⋅minmaxsum(P,m)。
(4) 证明不论第 2 行选择何种划分 Q,上述代码中的 while 循环最多重复 n 次。
(5) 描述所需的数据结构以使上述算法运行在 O(nlogm) 时间内,并解释如何使用它们。
题目描述
将由 n 个互异正整数构成的集合
P={x1,…,xn} 划分为 m 个两两不交、并集为 P 的集合
P1,…,Pm,其中 1<m<n;记划分为
Q=[P1,…,Pm]。对整数集合或整数栈 S,以
∥S∥ 表示元素总和,空集合或空栈的和为 0。定义
maxsum(Q)=imax∥Pi∥,
并以 minmaxsum(P,m) 表示所有 m 划分中上述最大和的最小值。
题中近似算法从任意划分开始,把每个 Pi 的元素压入栈 Si;循环选择总和最大的栈
Sj 和总和最小的栈 Sk。若
top(Sj)+∥Sk∥≥∥Sj∥,
则停止并返回 ∥Sj∥;否则将 Sj 的栈顶弹出并压入
Sk。回答下列问题。
(1)计算
minmaxsum({3,4,5,6},2)。
(2)证明
minmaxsum(P,m)≥m∥P∥.
(3)证明无论初始任意划分如何选择,算法返回值均满足
approx-minmaxsum(P,m)≤2minmaxsum(P,m).
(4)证明无论初始划分如何选择,while 循环最多重复 n 次。
(5)说明使算法达到 O(nlogm) 运行时间所需的数据结构及其用法。
Kai
(1)
To find minmaxsum({3,4,5,6},2), divide the set into two subsets so that the larger subset sum is minimized. The total sum is
3+4+5+6=18,
so every division has maximum subset sum at least 18/2=9. The relevant divisions include:
- P1={3,6},P2={4,5}, whose sums are 9,9;
- P1={3,5},P2={4,6}, whose sums are 8,10;
- P1={3,4},P2={5,6}, whose sums are 7,11.
The first division attains the lower bound. Hence
minmaxsum({3,4,5,6},2)=9.
(2)
Let P={x1,x2,…,xn}, and suppose we divide P into m subsets P1,P2,…,Pm. By definition:
minmaxsum(P,m)=Qminimax∥Pi∥
where Q is a possible division of P into m subsets.
For any division Q, the sum of all elements in P must equal the sum of the elements in all subsets:
i=1∑m∥Pi∥=∥P∥
Let M=maxi∥Pi∥. Then:
m⋅M≥∥P∥
because the total sum is distributed across m subsets, and the largest subset sum must be at least ∥P∥/m:
M≥m∥P∥
Since minmaxsum(P,m) is the minimum possible value of M, we have:
minmaxsum(P,m)≥m∥P∥
(3)
Let A=∥Sj∥, B=∥Sk∥, and x=top(Sj) when the algorithm stops. Then
For M=minmaxsum(P,m), every element satisfies x≤M, and
B≤∥P∥/m≤M by (2). Therefore
approx-minmaxsum(P,m)=A≤x+B≤2M.
(4)
Suppose an element x is moved from a stack of sum A to a minimum stack of sum B. The move condition gives
B+x<A,B<A−x.
Hence after the move every stack sum is at least B; the minimum stack sum never decreases. If x ever becomes the top of its stack again, that stack has sum B+x, while the current minimum is at least B. The stopping condition then holds, so x cannot be moved again. Each successful iteration therefore moves a distinct element, and there are at most n such iterations.
(5)
To achieve an O(nlogm) running time, use indexed priority queues for the stack sums:
- Store each stack Si normally, and store (∥Si∥,i) in both an indexed min-heap and an indexed max-heap. The minimum-sum and maximum-sum stacks can then be found in O(1) time.
- In each iteration, pop the top element from the maximum-sum stack and push it onto the minimum-sum stack. Only these two sums change, so update their keys in both heaps in O(logm) time. The stack operations themselves take O(1) time.
Initialization takes O(n+m) time. By (4), the loop runs at most n times, and each iteration costs O(logm). Since m<n, the total running time is O(nlogm).
Knowledge
贪心算法 集合划分 复杂度分析
重点词汇
- division: 划分
- priority queue: 优先队列
- minmax: 最小最大化
参考资料
- Cormen, T. H., Leiserson, C. E., Rivest, R. L., & Stein, C. (2009). Introduction to Algorithms (3rd ed.). MIT Press. Chapter 16: Greedy Algorithms.
- Kleinberg, J., & Tardos, É. (2005). Algorithm Design. Pearson. Chapter 6: Greedy Algorithms.