dp学习之BadNeighbors解法

这是来自Topcoder的一道动态规划有关的题目,原文如下
The old song declares “Go ahead and hate your neighbor”, and the residents of Onetinville have taken those words to heart. Every resident hates his next-door neighbors on both sides. Nobody is willing to live farther away from the town’s well than his neighbors, so the town has been arranged in a big circle around the well. Unfortunately, the town’s well is in disrepair and needs to be restored. You have been hired to collect donations for the Save Our Well fund.

Each of the town’s residents is willing to donate a certain amount, as specified in the int[] donations, which is listed in clockwise order around the well. However, nobody is willing to contribute to a fund to which his neighbor has also contributed. Next-door neighbors are always listed consecutively in donations, except that the first and last entries in donations are also for next-door neighbors. You must calculate and return the maximum amount of donations that can be collected.
题目大意就是:有一组首尾相连的数组,要求在不选取相邻的数据的前提下,挑选其中的数据使其和最大。
如:{10,3, 2, 5, 7, 8}
结果:19
分析:这组数据有如下可选取的可能10+2+7或3+5+8显然10+2+7更大(注意:因为是首尾相连所以第一个元素和最后一个也是相邻数据)
{11, 15}
结果:15

问题分析求解:
假设数据为x0,x2,…..,xn-1,并且这组数据首尾无关(为降低问题复杂度),用数组dp[n]存储在计算过程中

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值