One question per day (18) -- Book Purchasing (Dynamic Planning)

Source: Internet
Author: User

I. Problems

There are a total of five flags in the "Harry Potter" series. Assume that each volume is sold separately for 8 euros. If you purchase two different volumes at a time, you can deduct 5% of the fee, and the three volumes are more. Assume that the discount is as follows:

2 discount 5%

3 discounts: 10%

4 discounts: 20%

5 discounts: 25%

There is a certain demand for books, but different discounts can be bought through different book buying combinations.

Problem: An algorithm is designed to calculate the lowest price for a batch of books purchased by the reader.

 

Ii. Problem Analysis:

Greedy Policy

When the number of books is n <5, the books are directly purchased at a discount.

When the number of books is n> 5, the situation is as follows:

In this case, we can cite the situation of each combination, and analyze any situation (I, J, K, M, N ).

First, find 5 different types of books in all books. If yes, purchase at the discount price of 5 books.

Next, find out all four types of books in the remaining books. If yes, purchase at the discount price of the four books.

Find out all three types of books in the remaining books. If yes, purchase at the discount price of the three books.

Finally, find all the two types of books in the remaining books. If yes, purchase them at the discount price of the two books.

The remaining books are bought at full price.

If there is an inverse example using this method (greedy method), for example, when you buy 8 books, you can split it into 5 + 3, with a discount of 1.55, or 4 + 4, A discount of 1.6 is included in both cases. The first case can be ruled out by selecting the lowest discount.

Conclusion: The greedy policy is not desirable.

Dynamic Planning

To solve the problem with dynamic planning, you must first find the recursive formula of dynamic planning, because dynamic planning is a layer-by-layer recursion from top to bottom, and then a layer-by-layer solution from bottom to bottom! Finally, the final result is obtained based on the bottom-layer conclusions.

The price of the five-volume books is the same as 8 euro, so the effect of purchasing (, 0, 0) is the same as that of (, 0. Here, we can simplify the process to make it easier to discuss how to increase or decrease the number of books you have bought.

The parameters to be processed are the number of purchased volumes, so recursion must be related to these five parameters. You can sort the parameters in ascending order. The number of parameters not 0 is discussed to find all possible discount types. Then, the minimum price is obtained from the current discount type.

State transition equation:

(X1, x2, X3, X4, X5) indicates the number of purchased volumes. F (x1, x2, X3, X4, X5) indicates the lowest price. Where, X1
<X2 <X3 <X4 <X5

F (x1, x2, X3, X4, X5) = 0
; When all parameters are 0 (this is also the exit to exit recursion)


F (x1, x2, X3, X4, X5) =
Min {

5*8 * (1-25%) + f (X1-1, X2-1, X3-1, X4-1, X5-1)
// All parameters> 0

4*8 * (1-20%) + f (x1, X2-1, X3-1, X4-1)
// X2> 0

3*8 * (1-10%) + f (x1, x2, X3-1, X4-1, X5-1) // X3
> 0

2*8 * (1-5%) + f (x1, x2, X3, X4-1, X5-1) // X4
> 0


8 + f (x1, x2, X3, X4, X5-1) // X5
> 0

}

3. Dynamic Planning source code:

Source code:

# Include <stdlib. h >#include <iostream >#include <random> using namespace STD; # define random (x) (RAND () % x) const int large = 10000; int countre = 0, countdp = 0; // calculate the number of recursion times with dynamic planning, and count double s [10] [10] [10] [10] [10]; template <typename T> void rerank (t m [], int length) // insert sort {// For (INT I = 0; I <length; I ++) cout <m [I] <""; // cout <Endl; For (INT I = 1; I <length; I ++) {t tmp = m [I]; // very important; Int J; For (j = I-1; j> = 0 & TMP <m [J]; j --) {M [J + 1] = m [J];} m [J + 1] = TMP;} // For (INT I = 0; I <length; I ++) cout <m [I] <"";} double findmin (double T1, double T2, double T3, double T4, double t5) {double N [5] = {T1, T2, T3, T4, T5}; rerank (n, 5); Return N [0];} double bestbuy (INT X1, int X2, int X3, int X4, int X5) {countre ++; int I; int N [5] = {x1, x2, X3, X4, X5 }; rerank (n, 5); X1 = N [0]; x2 = N [1]; X3 = N [2]; X4 = N [3]; x5 = N [4]; If (s [X1] [X2] [X3] [X4] [X5]> 0) return s [X1] [X2] [X3] [X4] [X5]; countdp ++; /* X1 <X2 <X3 <X4 <X5 */If (N [0]> 0) {double solution = findmin (8 + bestbuy (x1, x2, X3, X4, x5-1), 2x8*0.95 + bestbuy (x1, x2, X3, X4-1, X5-1), 3x8*0.9 + bestbuy (x1, x2, x3-1, X4-1, X5-1), 4x8*0.8 + bestbuy (x1, x2-1, X3-1, X4-1, X5-1 ), 5*8*0.75 + bestbuy (x1-1, x2-1, X3-1, X4-1, X5-1 )); s [X1] [X2] [X3] [X4] [X5] = solution; return solution;} else if (n [0] = 0) & (N [1]> 0) {double solution = findmin (8 + bestbuy (x1, x2, X3, X4, X5-1 ), 2*8*0.95 + bestbuy (x1, x2, X3, X4-1, X5-1), 3*8*0.9 + bestbuy (x1, x2, x3-1, x4-1, X5-1), 4*8*0.8 + bestbuy (x1, x2-1, X3-1, X4-1, X5-1), large ); s [X1] [X2] [X3] [X4] [X5] = solution; return solution;} else if (n [0] = 0) & (N [1] = 0) & (N [2]> 0) {double solution = findmin (8 + bestbuy (x1, x2, X3, X4, x5-1), 2x8*0.95 + bestbuy (x1, x2, X3, X4-1, X5-1), 3x8*0.9 + bestbuy (x1, x2, x3-1, X4-1, X5-1), large, large); s [X1] [X2] [X3] [X4] [X5] = solution; return solution ;} else if (n [0] = 0) & (N [1] = 0) & (N [2] = 0) & (N [3]> 0) {double solution = findmin (8 + bestbuy (x1, x2, X3, X4, X5-1 ), 2*8*0.95 + bestbuy (x1, x2, X3, X4-1, X5-1), large ); s [X1] [X2] [X3] [X4] [X5] = solution; return solution;} else if (n [0] = 0) & (N [1] = 0) & (N [2] = 0) & (N [3] = 0) & (N [4]> 0) {double solution = 8.0 + bestbuy (x1, x2, X3, X4, X5-1 ); s [X1] [X2] [X3] [X4] [X5] = solution; return solution ;} else {s [X1] [X2] [X3] [X4] [X5] = 0; return 0 ;}} int main () {int n = 5; int A [10]; memset (S, 0, sizeof (s); For (INT I = 0; I <n; I ++) {A [I] = random (10); cout <A [I] <"" ;}cout <Endl; cout <bestbuy (A [0], A [1], a [2], a [3], a [4]) <Endl; For (INT I = 0; I <n; I ++) cout <A [I] <"; cout <Endl; cout <" Number of recursion: "<countre <Endl; cout <"number of dynamic planning recursive computations:" <countdp <Endl ;}

 

Using a 5-Dimensional S array to save the intermediate results requires a large amount of extra space.

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.