这是来自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]存储在计算过程中