BadNeighbors
来源:互联网 发布:怎么才能在淘宝上卖东西 编辑:程序博客网 时间:2024/06/07 01:35
Problem Statement
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.
Definition
Class: BadNeighbors
Method: maxDonations
Parameters: int[]
Returns: int
Method signature: int maxDonations(int[] donations)
(be sure your method is public)
Constraints
- donations contains between 2 and 40 elements, inclusive.
- Each element in donations is between 1 and 1000, inclusive.
Examples
0)
{ 10, 3, 2, 5, 7, 8 }
Returns: 19
The maximum donation is 19, achieved by 10+2+7. It would be better to take 10+5+8 except that the 10 and 8 donations are from neighbors.
1)
{ 11, 15 }
Returns: 15
2)
{ 7, 7, 7, 7, 7, 7, 7 }
Returns: 21
3)
{ 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 }
Returns: 16
4)
{ 94, 40, 49, 65, 21, 21, 106, 80, 92, 81, 679, 4, 61,
6, 237, 12, 72, 74, 29, 95, 265, 35, 47, 1, 61, 397,
52, 72, 37, 51, 1, 81, 45, 435, 7, 36, 57, 86, 81, 72 }
Returns: 2926
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.
This problem was used for:
2004 TCCC Online Round 4 - Division I, Level One
题目大意是需要捐款,然后每一个人如果他们的邻居捐款那么他就会不捐款,每个人都有一个对应的捐款值,要求求出最多的捐款数。
我们可以定义
一种不选
选了就是没有
这道题目还有一个关键的地方在于首尾的判断。这里可以把圆环转换成2条线段:
一条包含
另一条不包含
对这两条线段进行
#include <bits/stdc++.h>using namespace std;const int maxn=1e5+1;int n,a[maxn],d[maxn],f[maxn],key;char c;int main(){ while(cin>>a[++n]>>c); for(int i=1;i<n;i++) f[i]=max(f[i-1],f[i-2]+a[i]); for(int i=2;i<=n;i++) d[i]=max(d[i-1],d[i-2]+a[i]); cout<<max(f[n-1],d[n])<<endl; return 0;}
- BadNeighbors
- BadNeighbors
- BadNeighbors - 2004 TCCC Round 4
- dp学习之BadNeighbors解法
- BadNeighbors (TCCC '04 Round 4 Div1 250)
- 动态规划之LIS && ZigZag && BadNeighbors
- dp学习之BadNeighbors解法二(算法优化)
- 1002. A+B for Polynomials (25)
- js与flash交互
- JS关于计时器函数传参
- jquery实时监控textarea文本框中的字符(中文汉字及标点、英文字母及标点、数字)长度
- QT学习源网址转载1---QT多线程参数传递(信号与槽)方法
- BadNeighbors
- 健康一直都在,太一三和守护。
- NC反弹的小demo
- java中“53”个关键字(含2个保留字)
- 回形取数
- 剑指offer-第一个只出现一次的字符
- Java方法——方法定义,方法重载,参数和返回值
- 【笔记】关于EditPlus配置 Java 编译环境
- Qt中动态实现语言国际化