思考题15-9
首先我们在
L
表的最前和最后面分别加上
那么现在就有点类似矩阵链乘法问题了。设子问题
(i,j)
的拆分地方为
L[k],i<k<j
。拆分后变成两个子问题:前缀
(i,k)
包括
L[i+1,k−1]
,后缀
(k,j)
包括
L[k+1,j−1]
。定义
cost[i,j]
是子问题
(i,j)
的最优拆分:
BREAK-STRING(n,L)
prepend 0 to the start of L and append n to the end of L
m = L.length
sort L into increasing order
let cost[1..m,1..m] and break[1..m,1..m] be new tables
for i = 1 to m - 1
cost[i, i] = cost[i, i + 1] = 0
cost[m,m] = 0
for len = 3 to m
for i = 1 to m - len + 1
j = i + len - 1
cost[i, j] = ∞
for k = i + 1 to j - 1
if cost[i, k] + cost[k, j] < cost[i, j]
cost[i, j] = cost[i, k] + cost[k, j]
break[i, j] = k
cost[i, j] = cost[i, j] + L[j] - L[i]
print “The minimum cost of breaking the string is ” cost[1,m]
PRINT-BREAKS(L, break, 1,m)
PRINT-BREAKS(L, break, i, j)
if j - i ≥ 2
k = break[i, j]
print “Break at ” L[k]
PRINT-BREAKS(L, break, i, k)
PRINT-BREAKS(L, break, k, j)
总的时间复杂度是 Θ(n3) 。
思考题15-10
a) 很显然我们把钱投资到回报率最高的那种投资上面会得到更多的回报。
b) 令
f(k,i)
为在第
k
年我们把钱投资到第
c) 给出初始资金
d
和回报率表
INVESTMENT-STG(d,r):
Let c[0..n] be an array to store the money after ith year.
Let j[0..n] be the array that store which investment is selected in ith year.
for i = 0 to 10:
c[i] = 0;
j[i] = 0;
for k = 1 to 10:
for i = 1 to n:
fee = i == j[k - 1] ? f1 : f2;
new_sum = c[k - 1] * rik - fee;
if new_sum > c[k]:
c[k] = new_sum;
j[k] = i;
return c, j;
时间复杂度 O(n) 。
d) 找出一个反例即可。
思考题15-11
定义子问题
(k,s)
为在第
k
个月开始时,在剩余
1) 在上月剩下
s
台机器时,最少生产
2) 在上月剩下
s
台机器时,最多生产上界
最后一个月只需生产满足
L[n,s]
的台数即可。其他月生产的机器是弹性的,满足最优解就行。
INVENTORY-PLANNING(n,m, c,D, d, h)
let cost[1..n,0..D] and make[1..n,0..D] be new tables
// Compute cost[n, 0..D] and make[n, 0..D].
for s = 0 to D
f = max(dn - s, 0)
cost[n, s] = c·max(f - m, 0) + h(s + f - dn)
make[n, s] = f
// Compute cost[1..n-1,0..D] and make[1..n - 1,0..D].
U = dn
for k = n - 1 downto 1
U = U + dk
for s = 0 to D
cost[k, s] = ∞
for f = max(dk - s, 0) to U - s
val = cost[k + 1, s + f - dk] + c·max(f - m, 0) + h(s + f - dk)
if val < cost[k, s]
cost[k, s] = val
make[k, s] = f
print cost[1,0]
PRINT-PLAN(make, n, d)
PRINT-PLAN(make, n, d)
s = 0
for k = 1 to n
print “For month ” k “ manufacture ” make[k, s] “ machines”
s = s + make[k, s] - dk
INVENTORY-PLANNING
的时空复杂度分别是
O(nD2),O(nD)
。
思考题15-12
设球员
p
的签约费用和 VORP 分别是
由于先签约哪个位置的球员没有影响,我们假设从第一个位置签约到最后一个位置。对每个位置,要么签约一个队员,要么使用现有球员。假设签约位置 1 的球员
p
,我们剩下
定义一个球员签约费用和 VORP 的集合,集合中所有球员签约费用和 VORP 的总和就是要求的。记
(i,x)
为子问题:考虑位置为
i,i+1,...,N
并且总的费用
x
美元,什么样的集合使得 VORP 最大?一个有效的集合
当
i<N
时检查两个子问题并选择最优的一个,设
v[i,x]
为
(i,x)
中的最大 VORP。
S(i,x)
为打位置
i
且签约费用至多为
令 pij 表示在第 i 位置的第
FREE-AGENT-VORP(p,N,P,X)
let v[1..N][O..X] and who[1..N][0..X] be new tables
for x = 0 to X
v[N, x] = −∞
who[N, x] = 0
for k = 1 to P
if p_Nk.cost ≤ x and p_Nk.vorp > v[N,x]
v[N,x] = p_Nk.vorp
who[N,x] = k
for i = N - 1 downto 1
for x = 0 to X
v[i, x] = v[i + 1, x]
who[i, x] = 0
for k = 1 to P
if p_ik.cost ≤ x and v[i + 1, x - p_ik.cost] + p_ik.orp > v[i, x]
v[i, x] = v[i + 1, x - p_ik.cost] + p_ik.vorp
who[i, x] = k
print “The maximum value of VORP is ” v[1,X]
amt = X
for i = 1 to N
k = who[i, amt]
if k ≠ 0
print “sign player ” p_ik
amt = amt - p_ik.cost
print “The total money spent is ” X - amt
时间和空间复杂度分别是 Θ(NPX),Θ(NX) 。