Editorial for COCI '16 Contest 4 #3 Kas
Submitting an official solution before solving the problem yourself is a bannable offence.
The entire task comes down to correctly distributing the banknotes as described in the first paragraph of the task. So, we need to distribute some of banknotes into two parts so that the sum of the money in each of the two parts is equal, and the total sum of the money of the unused banknotes is the smallest possible.
Using a naive algorithm, we could have tried out every possible combination and get of total points. Since each banknote can end up with either Kile or Pogi or nobody, we can conclude that the time complexity of such algorithm is .
Similar to the previous thought process, let's imagine that we iterate respectively over the banknotes and try assigning them to Kile, Pogi, or nobody. In any step of such an algorithm, we should know which banknote we are currently processing and how much money have Kile and Pogi collected so far. Let be a function that returns the largest possible sum of money that Kile and Pogi can collect if after distributed banknotes Kile has kn, and Pogi has kn.
Evidently, where denotes the value of the banknote. Of course, if is different than , or in the contrary. If we implement such a solution using the technique of dynamic programming, we have constructed an algorithm of the complexity , where represents the sum of all banknotes.
To obtain all points, it was necessary to notice that it is sufficient to keep track in the state the absolute value of the difference of the amount of money Kile and Pogi have collected so far. We transition from state to states , and that represent, respectively, skipping of a banknote, assigning a banknote to the person currently having more money, and assigning a banknote to the person currently having less money. Since we have states, and the transition is constant, we can conclude that the algorithm is of the complexity , which is sufficient to obtain all points.
Even though this wasn't a requirement in the task, we advise you to think of an algorithm to solve this task with a memory limitation of 32 MB.
Comments