[Swust OJ 1139] -- Coin-row problem, swust -- coin-row

Source: Internet
Author: User

[Swust OJ 1139] -- Coin-row problem, swust -- coin-row

 

 

Link: http://acm.swust.edu.cn/contest/0226/problem/1139/

There is a row of n coins whose values are some positive integers c functions, c functions ,..., 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.
DescriptionTwo lines, the first line is n (0 <n <= 10000), and the second line is value of coin (0 <value <= 2 ^ 32 ). inputthe maximum amount of money. output
12 65 1 2 10 6 2
Sample Input
1 17
Sample OutputHintAlgorithm Text Book ~~ Idea: I guess this teacher talked about the dp algorithm. This set of questions is almost all about dp (also drunk) if it cannot be adjacent, consider the best state + current number corresponding to the first two numbers of the current bit, and compare it with the status of the previous number of the current number. The boundary dp [0] = 0, dp [1] = x [1] (array of x Data elements is stored starting from 1), the following dp equation dp [I] = max (dp [I-1], dp [I-2] + x [I]). For more information, see the code ~~~ 1 # include <iostream> 2 using namespace std; 3 int n, dp [10001], I, x [10001]; 4 # define max (a, B) a> B? A: B 5 int main () 6 {7 cin> n; 8 for (I = 1; I <= n; I ++) cin> x [I]; 9 dp [1] = x [1]; 10 for (I = 2; I <= n; I ++) 11 dp [I] = max (dp [I-1], dp [I-2] + x [I]); 12 cout <dp [n] <"\ r \ n"; 13 return 0; 14}View Code

Dp is so awesome, and a few lines of code have the answer ~~~

 

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.