[Swust OJ 1139]--Coin-row problem
2015-05-30 21:45
330 查看
题目链接: http://acm.swust.edu.cn/contest/0226/problem/1139/
There is a row of n coins whose values are some positive integers c₁, c₂,...,cn, not necessarily distinct. The goal is to pick up the maximum amount of money subject to the constraint that no two coins adjacent in the initial row can be picked up.
Description
Two lines, the first line is n (0< n <=10000), and the second line is value of coin(0< value <= 2^32).
Input
the maximum amount of money.
Output
Sample Input
Sample Output
Hint
Algorithm Text Book
题目大意:就是给你一排数字,不能够拿相邻的数字,问拿的数字的最大和为多少~~
思路:估计这老师才讲了dp 算法吧,这组题几乎全是dp(也是醉了)
不能够相邻那么就考虑当前位的前两个数对应的最好状态+当前数,和当前数的前一个数的状态比较,这里的边界dp[0]=0,dp[1]=x[1](x数据元素数组从1开始存贮),
那么可以得到如下dp方程 dp[i] = max(dp[i - 1], dp[i - 2] + x[i]);
具体的看代码理解吧~~~
View Code
dp就是这么牛逼,几行代码就整出了答案~~~
There is a row of n coins whose values are some positive integers c₁, c₂,...,cn, not necessarily distinct. The goal is to pick up the maximum amount of money subject to the constraint that no two coins adjacent in the initial row can be picked up.
Description
Two lines, the first line is n (0< n <=10000), and the second line is value of coin(0< value <= 2^32).
Input
the maximum amount of money.
Output
1 2 | 6 5 1 2 10 6 2 |
1 | 17 |
Hint
Algorithm Text Book
题目大意:就是给你一排数字,不能够拿相邻的数字,问拿的数字的最大和为多少~~
思路:估计这老师才讲了dp 算法吧,这组题几乎全是dp(也是醉了)
不能够相邻那么就考虑当前位的前两个数对应的最好状态+当前数,和当前数的前一个数的状态比较,这里的边界dp[0]=0,dp[1]=x[1](x数据元素数组从1开始存贮),
那么可以得到如下dp方程 dp[i] = max(dp[i - 1], dp[i - 2] + x[i]);
具体的看代码理解吧~~~
#include <iostream> using namespace std; int n, dp[10001], i, x[10001]; #define max(a,b) a>b?a:b int main() { cin >> n; for (i = 1; i <= n; i++) cin >> x[i]; dp[1] = x[1]; for (i = 2; i <= n; i++) dp[i] = max(dp[i - 1], dp[i - 2] + x[i]); cout << dp << "\r\n"; return 0; }
View Code
dp就是这么牛逼,几行代码就整出了答案~~~
相关文章推荐
- Joel 技术分享心得
- 【PS后期】一个蛋糕的故事
- Android 中双击退出
- Android 记录2 对于Android事件分发的理解
- JavaScript 开发的45个经典技巧
- 【末世旅行之C++】C++中的虚函数和纯虚函数的理解
- iOS layoutSubviews视图重绘总结
- 编程之美第二题 找连续数
- 观察者模式
- 设计模式——外观模式
- spring 中 applicationEvent的使用
- openlayers实现wfs属性查询和空间查询
- openlayers实现wfs属性查询和空间查询
- PHP Cookies
- PHP Cookies
- slf4j使用
- MZOI-一个人的旅行
- linux下source insight
- DJ's WebGL Tutorial 003--渲染一个三角形
- iOS测试中注意的地方