[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
Sample Input
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 ~~~