Editorial for COCI '15 Contest 6 #5 Krumpirko


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.

The solution worth 30\% of points is trivial and implements precisely what is required in the task: from all possible choices of divisions of potato bags in the stores, which is \mathcal O(2^N), choose the one with the minimal product of average prices where at least one half is of size L. The complexity of this solution is \mathcal O(2^N \times N). Let's now try to solve the task for all points.

Let's denote the total price of the potatoes in the first store as c_1 and the total number of potatoes as w_1, and analogously in the second store as c_2 and w_2, the product of the average prices is equal to c_1 \times c_2 / w_1 / w_2. Given the fact that the sum of prices and the sum of the number of potatoes is constant, we can modify this expression as c_1 \times (c-c_1) / (w_1 \times (w-w_1)). If we fix the parameter w_1, we can notice that this expression is minimal when c_1 is minimal as well. Now our task is to find the minimal c_1 for each w_1 such that the first store contains exactly L or N-L bags of potatoes. The minimal such c_1 is found using dynamic programming. Let f(n, w, l) be the smallest price when choosing exactly l bags of potatoes out of the first n bags of potatoes so that the chosen bags contain exactly w potatoes. The relation in this dynamic is left as an exercise to the reader. The total complexity of the solution is \mathcal O(wn^2).


Comments

There are no comments at the moment.