BadNeighbors - 2004 TCCC Round 4
来源:互联网 发布:网络机房有辐射吗 编辑:程序博客网 时间:2024/05/17 23:20
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.
动态规划。
状态:
d[i]表示前i个donation中以num[i]为结尾的最大的满足限制条件的donation序列和。
状态转移:
要想求d[i],就把i-1(因为不能相邻)前面的各个子序列的的d[j](0<=j<i-1)加上当前的donation值num[i],然后取最大就是当前的状态d[i]。这里要注意的是,最后一个数和第一个数也不能相邻,故保存一个是否以第一个数开头的标记数组label[]。当i等于n-1时要判断label[j]是否为1,如果为1,则continue。每次更新d[i]时也更新label[i]。
int maxDonations(int num[], int n){ int *d = new int[n]; int *label = new int[n]; int max_don = 0; label[0] = 1; for(int i = 0; i < n; i++) { d[i] = num[i]; for(int j = 0; j < i - 1; j++) { if(i == n-1) { if(label[j] == 1) continue; } if(d[j] + num[i] > d[i]) { d[i] = d[j] + num[i]; if(label[j] == 1) label[i] = 1; else label[i] = 0; } } if(d[i] > max_don) max_don = d[i]; } return max_don;}
- BadNeighbors - 2004 TCCC Round 4
- BadNeighbors (TCCC '04 Round 4 Div1 250)
- ChessMetric - 2003 TCCC Round 4
- 拓扑排序 FlowerGarden - 2004 TCCC Round 1
- DP ChessMetric 【TCCC'03 Round 4】
- BadNeighbors
- BadNeighbors
- TCCC Algo Qual Round 1(2007-08-19 02:59)
- TCCC Algo Qual Round (22007-08-21 20:51)
- TCCC Algo Round 1B(2007-08-30 20:54)
- TopCoder 250 points Tournaments 153-TCCC 07 Round 1C DIV 1 78.15/250 31.26%
- 2004 TCCC OL1 div1 1000(概率dp,简单)
- dp学习之BadNeighbors解法
- ZigZag - 2003 TCCC Semifinals 3
- 动态规划之LIS && ZigZag && BadNeighbors
- 【TopCoder】ZigZag - 2003 TCCC Semifinals 3
- dp学习之BadNeighbors解法二(算法优化)
- BestCoder Round #4 题解
- log4j总结
- Android获取图片资源的4种方式
- cin用法介绍(特别是学习了cin.clear和cin.sync)
- 在android布局中使用include和merge标签
- mac配置apache+php+mysql问题记录
- BadNeighbors - 2004 TCCC Round 4
- servlet生命周期
- oracle 存储过程 利用数组类型type实现批量插入数据库
- zoj 3819(牡丹江现场赛A题)
- UVA - 10763 Foreign Exchange
- Linux命令export - 设置或显示环境变量
- 黑马程序员_JAVA_IO(二)
- 算法导论-第九章-中位数和顺序统计量:最坏情况为线性时间的选择算法C++实现
- C++ primer 容易出错小记第一章