[DP] Rod-cutting problem

给一个长度为 n 的杆子,切成小段卖出去,价格根据小段的长度不同而不同。下面是一个例子

我们要通过切成小段卖出尽可能高的总价钱。问题是:How to decompose the problem?

Decomposition 的第一步是:第一刀切在哪?可以切在最左边(等于整根卖出去);可以切在位置1,位置2,。。。

关键的一点是,刀切下去后,左半段就不再切了,继续切右半段。切右半段就变成了一个subproblem。

Naive Recursion:

Top-down implementation:

Bottom-up implementation: 不容易构想,变量 j 是 subproblem‘s
size,从1递增到n,变量 i 用来把每个subproblem 拆分成更小的subproblem,而根据我们这种计算的顺序,当计算 size = j
的时候,< j 的 subproblem 已经计算完毕了。

时间: 2024-08-04 19:40:42

[DP] Rod-cutting problem的相关文章

rod cutting

for a rod of length i the price of it si pi,to cut the rod to earn more money package dynamic_programming; public class rod_cutting { int [] r; public int[] BTU_rod_cutting(int[] p,int n) { r = new int[n]; //r[n] is the most money of the //length n i

[Java 8] (9) Lambda表达式对递归的优化(下) - 使用备忘录模式(Memoization Pattern) .

使用备忘录模式(Memoization Pattern)提高性能 这个模式说白了,就是将需要进行大量计算的结果缓存起来,然后在下次需要的时候直接取得就好了.因此,底层只需要使用一个Map就够了. 但是需要注意的是,只有一组参数对应得到的是同一个值时,该模式才有用武之地. 在很多算法中,典型的比如分治法,动态规划(Dynamic Programming)等算法中,这个模式运用的十分广泛. 以动态规划来说,动态规划在求最优解的过程中,会将原有任务分解成若干个子任务,而这些子任务势必还会将自身分解成更

[Java 8] Lambda表达式对递归的优化(下) - 使用备忘录模式(Memoization Pattern)

使用备忘录模式(Memoization Pattern)提高性能 这个模式说白了,就是将需要进行大量计算的结果缓存起来,然后在下次需要的时候直接取得就好了.因此,底层只需要使用一个Map就够了. 但是需要注意的是,只有一组参数对应得到的是同一个值时,该模式才有用武之地. 在很多算法中,典型的比如分治法,动态规划(Dynamic Programming)等算法中,这个模式运用的十分广泛. 以动态规划来说,动态规划在求最优解的过程中,会将原有任务分解成若干个子任务,而这些子任务势必还会将自身分解成更

UVA 10003 Cutting Sticks(区间dp)

Description  Cutting Sticks  You have to cut a wood stick into pieces. The most affordable company, The Analog Cutting Machinery, Inc. (ACM), charges money according to the length of the stick being cut. Their procedure of work requires that they onl

【HDU 5909】 Tree Cutting (树形依赖型DP+点分治)

Tree Cutting Problem Description Byteasar has a tree T with n vertices conveniently labeled with 1,2,...,n. Each vertex of the tree has an integer value vi. The value of a non-empty tree T is equal to v1⊕v2⊕...⊕vn, where ⊕ denotes bitwise-xor. Now fo

(DP) UVA 10003

F - Cutting Sticks Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 10003 Appoint description:  System Crawler  (2015-01-31) Description  Cutting Sticks  You have to cut a wood stick into pieces. The m

UVA 10003 - Cutting Sticks

#include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; in

石子归并(dp)

石子归并 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 89  Solved: 22 [Submit][Status][Web Board] Description 现在有n堆石子,第i堆有ai个石子.现在要把这些石子合并成一堆,每次只能合并相邻两个,每次合并的代价是两堆石子的总石子数.求合并所有石子的最小代价. Input 第一行包含一个整数T(T<=50),表示数据组数. 每组数据第一行包含一个整数n(2<=n<=100),表示石子的

hdu 4352 数位dp + 状态压缩

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2265    Accepted Submission(s): 927 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

uva 10003

E - Cutting Sticks Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 10003 Description  Cutting Sticks  You have to cut a wood stick into pieces. The most affordable company, The Analog Cutting Machiner