Coin-row problem (1139)

Source: Internet
Author: User

DescriptionThere is a row of n coins whose values was 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, coins adjacent in the initial row Can is picked up.InputLines, the first line was N (0< n <=10000), and the second line is the value of coin (0< value <= 2^32).OutputThe maximum amount of money. Sample Input65 1 2 6 2Sample Output17 topic: is to give you a row of numbers, not able to take the adjacent numbers, ask the number of the largest and how much ~ ~Ideas:Can not be adjacent then consider the current bit of the first two number corresponding to the best state + current number, and the current number of the previous number of the state comparison, here the boundary dp[0]=0,dp[1]=x[1] (x data element array starting from 1 storage); Code:


#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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.