Editorial for COCI '12 Contest 1 #2 F7


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 sort the drivers in descending points order and select the K^\text{th} driver in that sorted order. We need to determine whether (s)he can be the World Champion.

We can construct the best possible final race scenario for that driver. In such a race, (s)he would earn N points, while the current champion would earn only 1 point, the current runner-up only 2 points and so on. Generally, the driver in position p < K earns p points; drivers in positions p > K are irrelevant since they cannot have more points than driver K in the constructed scenario.

Can driver K be the World Champion in this scenario? The answer is 'yes' if (s)he has a sum greater than all other drivers, that is, if the relation

\displaystyle a[K]+N \ge \max\{a[1]+1, a[2]+2, \dots, a[K-1]+K-1\}

is satisfied, where a[p] is the number of points that driver p in the descending sequence had before the final race.

The solution that computes the maximum above (let us denote it by mx(K)) for every K from scratch has complexity \mathcal O(N^2), which is too slow. Fortunately, the maximum is simple to compute gradually as needed: whenever the observed K is incremented, we can compare the previous maximum value with the current point value and update the maximum if needed. To formalize,

\displaystyle mx(K+1) = \max\{mx(K), a[K]+K\}

This reduces the complexity of finding the solution to \mathcal O(N). The total complexity is \mathcal O(N \log N) because of sorting.


Comments

There are no comments at the moment.