读书人

一个算法求解解决方法

发布时间: 2012-12-30 10:43:14 作者: rapoo

一个算法求解,
Suppose we have a rod of n inches to sell. You may sell it without cutting, or you may cut it into several pieces to sell. If you cut it, every piece must be a multiple of inches. The price of each piece depends on its length, but may not be linearly increasing with the length. For example, you may have the following price table for a rod up to 6 inches long.

Length i (inches) 123456
Price pi (dollars)144789

If we cut a rod of 6 inches into 2 pieces of 2 inches and 4 inches, then we can sell them for 4 + 7 = 11 (dollars).
Now, If we cut it into 3 pieces of 2 inches each, then we can sell for 3 4 = 12 (dollars). If we don’t cut it, then we can sell it for just 9 dollars.
Suppose the price table P[i] is given, 1 i n, please design a dynamic algorithm that cuts a rod of n inches into pieces such that we can get maximum revenue by selling these pieces. You need to present a pseudo code and analyze its complexity.
[解决办法]
贪心的问题,就相当于装在一个容积为n的容器,给出了从1到n不同体积对应的价格,求在装满的情况下如何装在能获得最大的价格。
应该是采用最大性价比优先的贪心策略,约束条件就是对所有要装入的体积求和结果为n。
[解决办法]
动态规划,可以用遗传算法求解.
Max(Price)=∑(L*P)
L∈[1,6]
一次线性方程的最优解

编码长度3,初始样本4,变异位置2....固定套路,8个关键环节

练习一下遗传算法吧,
好处:
1将来有用,比其他的算法有通用价值.
2套路简单,容易上手
3可以多线程并行计算
4和问题本身逻辑无关,是用可能解去选择,杂交,变异得到的
5得到较优解的概率是1
6适合多参数复杂问题求解(可离散化)
[解决办法]
顶#2
f(0) = 0
f(n) = Max(Price(n), Max(f(i) + f(j))) 其中 i + j = n 0 < i,j < n
复杂度 时间: O(n^2) 空间: O(n)

读书人网 >软件架构设计

热点推荐