Editorial for TLE '16 Contest 4 P5 - Buying Gifts
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
For the 5% subtask, there is one gift type. Sort the shops according to their prices, and pick the cheapest shops. The cost of one gift is the shop's price.
Time complexity: or
For the 15% subtask, there are exactly two gift prices. This was intended to be an easier version of the 60% subtask.
Time complexity:
For the 20% subtask, it is possible to choose out of the possible stores. For each combination, calculate the total price if these stores were chosen. The answer is the minimum total price out of all the combinations.
Time complexity:
There are two different ways to approach the 60% subtask.
The first approach is to notice that there are at most possible combinations of gift prices. For each possible list of prices, it is possible to count the number of stores that can be used. This simplifies to calculating a -dimensional prefix sum array, which takes for the inclusion-exclusion approach, and for the dimension-by-dimension approach. Out of all the combinations, the elements with a value greater/equal to should be considered for the final answer, since these elements allow you to visit at least shops.
Time complexity:
The second approach also uses the fact that there are at most combinations of gift prices. The first gift's price is set to one of the possible values. Then the second gift's price is set, and so on until a final list of prices is reached. This can be done recursively. However, this approach might be very slow, so bitmask can be used to decrease runtime. A shop's availability can be stored as a bit in memory, which allows for quick calculations and bit counting. Every time a price is set, some random shops become unavailable (due to the price being way too low for the shop). A recursive implementation can be made extremely fast by removing branches that cannot yield any solution / a better solution. One drawback is that the first two subtasks need to be coded separately.
Time complexity:
Comments