Editorial for COCI '16 Contest 5 #2 Pareto


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.

We will sort the amounts descendingly by size, from the largest to the smallest one. For each "prefix" that consists of K richest clients, we will calculate the corresponding numbers A and B, the share of that prefix in the number of accounts (A = K / N \times 100\%) and the total share of the money for that prefix:

\displaystyle B = \frac{\text{the sum of }K\text{ largest amounts}}{\text{the sum of all amounts}} \times 100\%

We now check if the difference B-A is the largest so far: if it is, we update the temporary variables A_{best}, B_{best}, the ones we output in the end.

Given the size of the array, we need to efficiently sort it, in the complexity \mathcal O(N \log N), and then efficiently calculate the sum of prefixes. The easiest way to do so is to calculate the sum of K-prefix by adding the K^\text{th} element to the previously calculated (K-1)-prefix.


Comments

There are no comments at the moment.