Cutting a Rod
Given a rod of length n inches and an array of prices that contains prices of all pieces of size smaller than n.Determine the maximum value obtainable by cutting up the rod and selling the pieces. For example, if length of the
rod is 8 and the values of different pieces are given as following, then the maximum obtainable value is 22 (by cutting in two pieces of lengths 2 and 6)
length | 1 2 3 4 5 6 7 8
--------------------------------------------
price | 1 5 8 9 10 17 17 20
And if the prices are as following, then the maximum obtainable value is 24 (by cutting in eight pieces of length 1)
length | 1 2 3 4 5 6 7 8
--------------------------------------------
price | 3 5 8 9 10 17 17 20
The naive solution for this problem is to generate all configurations of different pieces and find the highest priced configuration. This solution is exponential in term of time complexity. Let us see how this problem possesses both important properties
of a Dynamic Programming (DP) Problem and can efficiently solved using Dynamic Programming.
Optimal Substructure:
We can get the best price by making a cut at different positions and comparing the values obtained after a cut. We can recursively call the same function for a piece obtained after a cut.
Let cutRoad(n) be the required (best possible price) value for a rod of lenght n. cutRod(n) can be written as following.
cutRod(n) = max(price[i] + cutRod(n-i-1)) for all i in {0, 1 .. n-1}
要根据上面的公式进行思考,形成抽象思维,和系统思维。
这种抽象能力非常难,也非常重要,直接决定了效率,甚至能否做出来的问题。
优秀的程序员和普通程序员之间的差距也许就从抽象思维能力中分出来了。
不过慢慢对动态规划法和递归回溯法熟悉了,其实就可以撇开什么公式和递归回溯,直接从表入手,填表,把表翻译为程序也是个很不错的做法。
程序1:
int cutRod(int price[], int n)
{
vector<int> ta(n+1);
for (int i = 1; i <= n; i++)
{
ta[i] = price[i-1];
for (int j = 1; j < i/2+1; j++)
{
ta[i] = max(ta[i], ta[j]+ta[i-j]);
}
}
return ta[n];
}
程序2:动态规划和递归
int cutRodDP(int price[], int n)
{
vector<int> ta(n+1);
for (int len = 1; len <= n; len++)
{//这里代表长度
int maxPrice = price[len-1];//长度问len的时候,值cut一段的价格
for (int cut = len-2; cut >= 0; cut--)
{//price[cut]代表cut出了cut长度的价格,然后余下的继续cut
maxPrice = max(maxPrice, price[cut] + ta[len-cut-1]);
}
ta[len] = maxPrice;
}
return ta[n];
}
/* Returns the best obtainable price for a rod of length n and
price[] as prices of different pieces */
int cutRod(int price[], int n)
{
if (n <= 0)
return 0;
int max_val = INT_MIN;
// Recursively cut the rod in different pieces and compare different
// configurations
for (int i = 0; i<n; i++)
max_val = max(max_val, price[i] + cutRod(price, n-i-1));
return max_val;
}
int main()
{
int arr[] = {1, 5, 8, 9, 10, 17, 17, 20};
int size = sizeof(arr)/sizeof(arr[0]);
printf("Maximum Obtainable Value is %d\n", cutRod(arr, size));
cout<<cutRodDP(arr,size)<<endl;
system("pause");
return 0;
}
分享到:
相关推荐
2022最新版:GEEKS V1.0.10主题:在线学习市场WordPress主题.rar
geeks-for-geeks-solutions:有关Geeks-for-Geeks的问题的解决方案。解决方案可用于C ++
Geeks2TheRescue:使用MERN的Web App,旨在连接雄心勃勃的开发人员
geeks_algorithms 极客算法编码问题
带有React JS的WebApp样板 要求: 确保您使用的是节点版本10 安装软件包: $ npm install 创建一个.env文件: $ cp .env.example .env 开始编码! 以及具有实时重新加载功能的webpack开发服务器(适用于Windows...
夏季极客提交 这个Web应用程序被配制成在Innnovaccer SDE实习分配的一部分,使用的NodeJS,MongoDB的,NodeMailer的电子邮件和Nexmo的手机信息,根据。 预习 报到表格 结帐电子邮件 使用的技术 ...
geeks for geeks上的资源,大家下载下,我攒点几分,呵呵。
欢迎来到4Geeks学院 <\编码时间>内容关于。创始人。团队。奖项。校友关于4Geeks是一家大公司,感觉就像一家小公司;每个人都可以访问,我们喜欢与人接触,我们相信量身定制的教育,而不会离开您的工作,并且100%...
:sparkles: gloomhaven-geeks :sparkles: 这是一个使用Git作为的网站。 它是在不到一分钟的时间内使用创建的。 您可以像这样,或探索一些变体。 如何不同: :artist_palette: 看 :pencil: 内容管理系统 :gear: 静态...
极客换极客 Geeks-For-Geeks 实习文章 使用 scapy 嗅探数据包 - 使用 Javascript 和 DOM 创建棋盘模式 - 如何将链接从一个 iframe 加载到另一个 iframe -
Geeks for Geeks的DSA练习: :
本题的难度并不在最短路径本身这个算法,而是在于堆的操作: 1 使用双重指针操作堆的节点,可以省去直接复制操作堆节点,提高效率,并且这才是有效操作动态地址数据的方法,不用双重指针,我思考了下,觉得更加不好...
Geeks3D Furmark(OpenGL显卡基准测试工具)V1.17.1 中文官方版
Hardware Hacking Projects for Geeks 英文chm 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除
A HashMap for Geeks and normal programmers.
geek极客们都用什么神器,这篇文章给了你答案。
Mac OS X For Unix Geeks 2003
Ubuntu for Non-Geeks (Fourth Edition)
UAT-a-Geeks 这是#UAT-a-Geeks League iOT概念证明的官方存储库,使用NodeJS和Arduino
Cooking.for.Geeks, hope all programer have good food