Section 1.4: Buying book questions

Source: Internet
Author: User
Author: Shide        every time after reading "The beauty of programming" in the question, want to personally calculate or in-depth thinking, all feel the time passed quickly, often one or two hours, if the code again to knock again, It may take longer, but it's not clear what the brains of the guys who interviewed through Microsoft are doing, the preface to the book mentions that they have 45 minutes each interview and write a program. In my opinion, it might be possible to control the CPU curve or the Chinese chess problem. If it's a book discount, I think it's really not easy, especially if the interviewer gets into the problem of the greedy solution instead of the dynamic programming algorithm, because I wrote this article about 5 hours:-(. But I think as long as the study is not a waste of time, today on the Internet to see Microsoft's campus recruitment site has been updated, and so I read the book, to cast a resume to try:-). 1 problem description and analysis        buy book discount the description is that a publisher of the "Harry Potter" series has 5 volumes, each is sold 8 yuan, if the reader to buy a different K (k>=2) volume, You can enjoy a different discount, as shown below:

The problem is how to calculate the maximum number of discounts given an order. The dynamic programming solution given in the book is no longer discussed here. However, there are two issues that need to be watched separately: (1) If the order is described as (x1,x2,x3,x4,x5), where x1-x5 is the number of the ordered number, which is the number of the volume, namely the first volume X1, the second volume X2, ..., volume 5th X5 ben; If the order is: (X3,X2, X4,X5,X1), the first volume X3, the second volume X2, ..., volume V X1. We can easily see that because each book has the same price, the discount is just about how to choose, not the book. Therefore, the maximum number of discounts on the above two orders is the same, which allows us to use a uniform method (y1,y2,y3,y4,y5), where y1≥y1≥y1≥y1≥y5, to represent an order. The authors set the price of each book to be the same in order to simplify the problem, because if the price of each book is different, the problem becomes more complicated, then we can't just think about selecting a few books, but also consider which ones to choose. (2) The book says that for a selection of 4 to 2 books at a time, (take 3 books for example) just consider the case of F (y1-1,y2-1,y3-1,y4,y5) (Note that f is a calculation function for the order price) and say "this option ensures that the current discount is selected The rest of the book is the most diverse, and it is better than any other combination. I think this conclusion is not obvious, the next alternative to the variety of books can prove that the problem is better than other children. I do not agree that the solution to this problem is a multi-step process of choice, so it is necessary to obtain this conclusion in strict proof. If this condition is not tenable, then the recursive formula given in the book is not tenable, that is, it cannot be proved that the structure of the optimized substructure is established. Therefore, for such an important detail, the book should give a strict proof rather than a sentence. Just start to get the problem when the conditioned to think can use greedy algorithm, that is, every time as far as possible according to the maximum discount to fetch books. The book gives a counter example: the given order is (2,2,2,1,1), according to the greedy algorithm, our choice is 5+3, the discount is 5*0.25+3*0.10=1.55, and if the 4+4 mode, the discount number is 2*4*0.20=1.6. This is clearly a violation of the greedy rules. So in the book, another analysis is used in solution two, the author calculates the number of books in the order in the [1-10] range, a variety of different selection methods can get the maximum discount number: This number of possible decomposition of the corresponding discount for 2-5, directly at the discount purchase 2 3 4 5 5% 10% 20% 25% =4+2 =3+3 =2+2+2 0.9 0.6 0.3 7 =5+2 =4+3 1.35 1.1 8 =5+3 =4+4 =3+3+2 =2+2+2+2 5*25%+3*10%=1.55 4*20%+4*20%=1.6 0.7 0.4 9 =5+4 =5+2+2 =4+3+2 =3+3+3 2.05 1.45 1.2 0.9 10 =5+5 =4+ 4+2 =4+3+3 =2+2+2+2+2 2.5 1.7 1.4 0.5 Table 1-2 discount calculation table look at the discount calculation table given above, we can analyze it briefly. In fact, the reason for this counter case is that 3 of the book's Discount quantity and 4 book discount quantity gap caused by too much. In fact, as long as we change the subject slightly, change the discount of three books to 15%, and get the discount shown in table 1-2, we will find that the greedy algorithm magically takes effect. (For the sake of clarity, I put the changes before and after the revised discount calculation to a table) so if the first author gives a discount of three book 15%, it can be seen from the table that the results obtained by using the original greedy algorithm are right and may be associated with the conclusion that the greedy algorithm is effective. I'm a little scared. Obviously, if you can apply the greedy algorithm, the greedy choice should not be limited to a certain amount of discount set. And if "Harry Potter" is not 5 volumes but m volumes, the expansion of the discount list is likely to produce more counter cases. The number of possible decomposition of this number of original discount for 2-5 copies, directly at the discount to purchase 2 3 4 5 5% 10% 20% 25% 5% 15% 20% 25% =4+2 =3+3 =2+2+2 6 0.9 0.6 0.3 0.9 0.9 7 =5+2 =4+3 1.35 1.1 1.35 1.25 8 =5+3 =4+4 =3+3+2 =2+2+2+2 5*25%+3*10%=1.55 4*20%+4*20%=1.6 0.7 0.4 5*25%+3*15%=1.7 1.6 3* 15%*2+2*10%=1.0 0.4 9 =5+4 =5+2+2 =4+3+2 =3+3+3 2.05 1.45 1.2 0.9 2.05 1.45 0.8+0.45+0.1=1.35 0.45*3=1.35 10 =5+5 =4+4+2 = 4+3+3 =2+2+2+2+2 2.5 1.7 1.4 0.5 2.5 1.7 1.7 0.5 table 1-3 a discount list that matches the greedy strategy so the author wants to divide the order of excess 10 into order groups that are less than 10, and add up the maximum discount for each group to get the global optimal solution. will be described below. In the end, the author tells the way to modify the greedy strategy, but INot too understanding, and there are some mistakes, the most important thing is that the author finally failed to prove its correctness, I will not delve into. The application of the analysis greedy algorithm of 2 greedy algorithm has two necessary conditions, namely optimizing substructure and greedy selectivity. The first property because the dynamic programming algorithm has been proved to be applicable, the optimization substructure is obviously established (if the dynamic programming in the book is recursive). Now need to prove its greedy selectivity, that is, how to "greedy" to choose. Obviously every time to find the largest number of discounts to deal with the greedy method is not feasible, then the greedy method is really not good or we "greed" is not correct. We'll analyze it below. The implication of greedy selectivity is that a global optimal solution can be achieved by local optimal selection, in other words, when considering the choice, we only consider the best choice for the current problem without considering the result of the child problem. The current selection made by the greedy algorithm may depend on all the choices that have been made, but not on the choice to be made or the solution of the child problem. Therefore, the greedy strategy is usually top-down, one by one to make the greedy choice, constantly to the given problem to a smaller problem. Of course, before that we have to prove that the greedy choices made at each step will eventually produce a global optimal solution, which is the crux of the matter. The idea of the grouping given in the book can be used for reference, but it is obvious that the directional error has been made. Because the key of the greedy algorithm is "choice", that is, from the current state to "greedy" from the multiple seed state to choose a "current" optimal solution, and by its greedy selectivity so that the final solution is just the optimal solution. Since the end of the book is the (modified) greedy algorithm, the whole problem should not be divided into several groups to execute. This is because the last selection of the greedy algorithm is continuous with the next selection, and it is not possible to disassemble them, and if the result is to be disassembled and then added, it is also necessary to prove that the split is the best split of the result. So our goal should ultimately be to find out how to "greed." In fact, the author's intention is right, but the actual goal should be defined as limiting the impact of this choice on subsequent choices, or the impact of this selection is limited to the next selection, which is also the reason for table 1-1 to calculate only 10 (two choices are up to 10 books), but the impact of the next selection may affect next choice , so we can't just take these choices apart and then add them together, and we can only finish the selection one time. 2.1 Special Circumstances first, let's analyze the order shown in Fig. 2-1, and it is clear that the 15 books in the frame of the blue box should be treated according to the discount of 5 books. The reason we "dare" to do so is that this process will not affect the next "choice". That is, if the scope of the blue box is expanded to a higher level, once the selection will make the last two books the number of 0, and the next time the choice of up to only 3 book discount can be selected, equivalent to indirectTo select the "5+3" mode. And if the scope of the blue box as shown in Figure 2-2 (left), this two processing we can also have "4+4" mode to choose, and this is the best choice. Therefore, when our choice does not reduce the number of columns, we can be assured to boldly choose the most profitable choice of the law; also in Fig. 2-1 (note: The left and right two pictures are not associated, are two different orders), as the fifth column is gone, the books in the blue box should still be treated in accordance with the left figure, Only this time we have a discount of 4 books. Think again, if we don't choose the one with the biggest discount (that is, the number of books), according to the definition of the dynamic programming sub-problem given in the book: If 4 books are taken from the previous four, 3 books are taken from the first three columns, and so on, we call them "minimum" (with the book "The smallest representation"). As we can see, this time not choosing the biggest discount is only the most discounted option to "postpone" it. Because the greedy algorithm every step of the choice should be the current situation of the optimal choice, that is, we should be as much as possible at each step to get discounts, rather than the discount should be "postponed" and lead to our choice is not the best choice. In other words, we should make sure that the next choice of books should not be more than the number of this choice, that is, we can allow two times the choice is 5+3 or 4+4, but must not allow 3+5 to appear. The author's Table 1-1 also confirms this, and we can see that all the splits in the table are sorted in descending order, that is, the maximum number that can be selected next time does not exceed the number of selected books.  

  For example, in the left image of Figure 2-2, if we select only 3 books at the bottom of this selection, then the minimum defined in the previous paragraph should be f (y1-1,y2-1,y3-1,y4,y5), so the next time we can still choose 5 books, More than three books selected this time, this is not supposed to happen. And if we choose 5 Books, the next time there are only 3 books to choose from, which is obviously possible, but not optimal, if we choose 4+4. Obviously it does not violate the rules we have just set and the biggest discount. Our question here is how to modify the original greedy definition so that it can help us pick out the maximum discount option. After dealing with some special cases, our order changed to the picture shown on the left of figure 2-2. And before we define: The number of the selected book should not be smaller than the maximum number of the next available book (in which, the book is based on the dynamic planning method given by the selection method). In other words, we should not choose the maximum discount to "postpone" the execution, and make this choice is not the current look "optimal." Second, this selection also makes the effect of this choice limited to the next time (that is, the range in the red box in the diagram), and does not "spread" to the selection after the next selection, such as the next selection. At the same time, the next selection will only be affected by the next choice, and we have succeeded in preventing the impact of this choice on the whole. 2.2 Modified greedy algorithm for the case shown on the left of figure 2-2, given the above conditions, we currently have 5 and 42 options (of course, if the volume of the book is m>5, maybe more options), how can we get the maximum discount without triggering a greedy counter case? My answer is "check the table". At this point, we should look at table 1-2 to find the most current option with the largest number of discounts. We can see that because of 5+3<4+4, so we should choose 4 Books this time. We also need to verify that next time we choose we need to select 4 books. At this point, as shown in Figure 2-2, our choice is only 4+3, because if this time we choose 3 Books, the next time we can choose 4 books, this is inconsistent with our rules. So our greedy choice is right. The rest of the books can be dealt with in the two cases mentioned above.

  After modification, the greedy algorithm's spatial complexity becomes the cost of calculating the table shown in 1-2, and the time complexity of the modified greedy algorithm is very intuitive, the number of times we should make a choice (including a look-up table operation), that is, O (Y1). 3 The rules of the greedy algorithm for summarizing the problem are: 1 The number of the selected books should not be smaller than the maximum number of the next available book (of which the book is to be chosen according to the dynamic programming method given in the book). 2 You should check the table before each selection, and select the one that makes the most of the most recent two discounts as the choice (because we make the impact of this selection controlled within two choices). The situation in the blue box in the left figure of Fig. 2-1 can ultimately be summed up as one of the conditions analyzed later. That is, because the next time only 5 can choose (less than five will violate rule 1), so this time choose 5 books. Also, when the volume of the book Changes to M (m>5), the number of calculations required in table 1-2 is correspondingly larger. But in fact, table 1-2 can be simplified, for example, when we look at 8 books, 3+3+2 and 2+2+2+2 don't really have to think about it, because the existence of rule 1 makes it impossible to choose 3 books, as shown in the left figure of figure 2-2. Even if for example, if we choose 3 Books, the next time we can only choose 3 books, we will return to the processing of figure 2-2 in the blue box of the left figure. Finally, if the volume of the book is M, the number of rows in our table is only 2M, because the effect of our choice is limited to this and next time, the number of books involved will not exceed 2M. 4 "The beauty of programming" This question Errata [1] P33, the 4+2 discount for this number 6 should be 4*20%+2*5%=0.9 instead of 1.1 [2] P34, the last paragraph, 第2-3 Line said the original "greedy strategy suggested we buy Y5 this volume five, y4-y5 this volume four, y3-y4 this volume three, Y2-y3 this volume Two and y1-y2 this volume a ", for orders (2,2,2,1,1), of which the first three volumes of two per volume, the last two volumes per volume, the greedy rule should be 5+3, that is, the first choice should be a roll to get one. And the article said that the fourth volume take y4-y5=0. So I don't think that's a proper word. And I do not understand the content of the following paragraph, I personally feel that there is a need to amend. [3] The same is P34 last paragraph, the last three lines. Although I do not know the meaning of this paragraph, but still can see an obvious mistake, that is the last three lines cited in the example mentioned "to buy 3 volumes first, 2 Volume II ..." and in the end said, "The adjusted greedy strategy tells us that we should buy three copies of the four volumes, three two rolls ..." and that the second volume is two. , but said to buy 3 of the second volume, I really do not know how this greedy strategy is "told." I suggest that the antiJust because the price of the book leads to the book on the order is not important, it is better not to use the first few volumes, instead of the first few columns more appropriate.

(original link: http://blog.csdn.net/kabini/archive/2008/04/16/2296943.aspx)

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.