Editorial for COCI '15 Contest 1 #2 Akcija


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Let us first notice that it doesn't make sense to sort the books in groups that contain less than 3 books because the discount is only valid for a group consisting of 3 books (to refresh our memory, the discount said that we'd get the cheapest of 3 books for free). This means that we need to sort all the books into groups of 3, so that there's only one group that has less than 3 books in the end (because the number of books that the customer decided to purchase is not a multiple of the number 3).

Let us now find the two most expensive books from the set of all books (their prices can be equal to each other). Because there is no other book more expensive than them, we can't create a group where we'd get a discount for those two books (because they are the most expensive ones), but we can then take the third most expensive book and put it in the group with the two most expensive ones. This way we'll get the discount for the third most expensive book. Now we have decreased the initial set of books by three.

By repeating this procedure while we still have books in the set and by ensuring that we get the discount for the most expensive book (the third most expensive book out of all the remaining ones) in every grouping, we make sure that the final price is going to be the minimal possible. If the number of books is not a multiple of the number 3, that means that we'll have 1 or 2 books in the last grouping and we'll have to pay for both, but that doesn't undermine the described algorithm.


Comments

There are no comments at the moment.