View the problem at POJ. It’s a problem from a contest in Northwestern Europe, 2002.
This is a classical Dynamic-Programing problem. The sate transformation equation is:
f(i) means the lowest possible price needed to buy pearls from class 1 to class
a(i) means the number of pearls needed to buy in class
p(i) menas the price of each peal in class
c means the number of categories.
The initial condition is:
The answer to this problem is:
You can iteratively calculate
f(i), ie from
f(c), or recursively calculate
f(i), with recording which
f(i) has been calculated.
If you want to prove the correctness of the state transformation, continue to the following parts.
All pearls in the same class should be bought in a unit. Prove: Assume we break them into two parts, and buy one part in class
i, and buy another in class
j. Without lost of generous, let
i < j, if we buy all of them in class
i, we will pay less money. So this is not the best plan, and we should buy all pearls in the same class in one class.
If we buy pearls, which is originally in class
i, in a higher class
j, then we must buy all pearls originally in class
k (i < k < j)in class
j. Prove: Assume there exists some pearls in class k (i < k < j) that is not bought in class
j. Then they must be bought in class
k, or in class
p (k< p < j), or in class
q (q > j). If they are bought in class
k, then if the pearls in class
iare bought in class
k, we will pay less money. So this is not a best plan. If they are bought in class
q, then if they are bought in class
j, we will pay less money, so this is not an optimistic plan, either. If they are bought in class
p, then there exists some pearls in class
p (k < p < j)that is not bought in class
j, and then we replace
p, we get the same proposition as the original proposition. Iteratively prove this proposition, and finally this case will falls into one of the above two cases. (Because
pis monotone increasing and
jis a limited number, so it’s convergence.) As all above cases results a contradiction, the assume is not true. Hence it is proved.
Source Code to resolve this problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44