Dynamic Programming
- Rod-cutting Problem: Given a rod of total length N inches and a table of selling prices P L P_L PL for lengths L=1,2,⋯,M. You are asked to find the maximum revenue R N R_N RN obtainable by cutting up the rod and selling the pieces. For example, based on the following table of prices, if we are to sell an 8-inch rod, the optimal solution is to cut it into two pieces of lengths 2 and 6, which produces revenue R 8 = P 2 + P 6 = 5 + 17 = 22 R_8=P_2+P_6=5+17=22 R8=P2+P6=5+17=22. And if we are to sell a 3-inch rod, the best way is not to cut it at all.
Length L | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|---|
Price P L P_L PL | 1 | 5 | 8 | 9 | 10 | 17 | 17 | 20 | 23 | 28 |
Which one of the following statements is FALSE?
A. This problem can be solved by dynamic programming
B. The time complexity of this algorithm is O ( N 2 ) O(N^2) O(N2)
C. If N≤M, we have R N = m a x ( P N , m a x 1 ≤ i < N ( R i + R N − i ) ) R_N=max(P_N,max_{1≤i<N}(R_i+R_{N−i})) RN=max(PN,max1≤i<N(Ri+RN−i))
D. If N>M, we have R N = m a x 1 ≤ i < N ( R i + R N − M ) R_N=max_{1≤i<N}(R_i+R_{N−M}) RN=max1≤i<N(Ri+RN−M)
分析:
对于任意一根长度为L的钢管,如果最优解包括一段长度为5的钢管,那么切下5单位长度后的钢管的最优解也是长度为L-5的钢管最优解。
问题的最优解包含相关子问题的最优解,这些子问题我们可以独立求解。
所以我们可以用动态规划的方式写这道题,递推关系式应该是
当 N > M N\gt M N>M, R N = m a x 1 ≤ i < N ( R i + R N − i ) R_N=max_{1≤i<N}(R_i+R_{N−i}) RN=max1≤i<N(Ri+RN−i)
当 N ≤ M N\le M N≤M, R N = m a x ( P N , m a x 1 ≤ i < N ( R i + R N − i ) ) R_N=max(P_N,max_{1≤i<N}(R_i+R_{N−i})) RN=max(PN,max1≤i<N(Ri+RN−i))
问题规模是指数级别的,所以选D.
- In dynamic programming, we derive a recurrence relation for the solution to one subproblem in terms of solutions to other subproblems. To turn this relation into a bottom up dynamic programming algorithm, we need an order to fill in the solution cells in a table, such that all needed subproblems are solved before solving a subproblem. Among the following relations, which one is impossible to be computed?
解题思路
- Given a recurrence equation f i , j , k = f i , j + 1 , k + m i n 0 ≤ l ≤ k ( f i − 1 , j , l + w j , l ) f_{i,j,k}=f_{i,j+1,k}+min_{0≤l≤k}(f_{i−1,j,l}+w_{j,l}){} fi,j,k=fi,j+1,k+min0≤l≤k(fi−1,j,l+wj,l). To solve this equation in an iterative way, we cannot fill up a table as follows:
此题相当于给定计算顺序,判断是否合理.(答案选B)