Coin-row problem


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.

输入
Two lines, the first line is n (0< n <=10000), and the second line is value of coin(0< value <= 2^32).
输出
the maximum amount of money.
样例输入
6
5 1 2 10 6 2
样例输出
17

思路:估计这老师才讲了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[n] << "\r\n";  
        return 0;  
    }  

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值