首页 > 其他 > 详细

Coin-row problem(1139)

时间:2015-11-08 19:25:21      阅读:301      评论:0      收藏:0      [点我收藏+]
 
 Description
        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. 
Input
        Two lines, the first line is n (0< n <=10000), and the second line is value of coin(0< value <= 2^32).
Output
         the maximum amount of money.
Sample Input
6
5 1 2 10 6 2
Sample Output
17
 题目大意:就是给你一排数字,不能够拿相邻的数字,问拿的数字的最大和为多少~~
  思路:不能够相邻那么就考虑当前位的前两个数对应的最好状态+当前数,和当前数的前一个数的状态比较,这里的边界dp[0]=0,dp[1]=x[1](x数据元素数组从1开始存贮);
代码:
 


#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[n] <<"\r\n";
return 0;
}

Coin-row problem(1139)

原文:http://www.cnblogs.com/FENGXUUEILIN/p/4947829.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!