Editorial for CEOI '17 P2 - Sure Bet


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 problem is asking us to maximize the value min(ainanb,bjnanb), where ai represents the odds for placed bets on the first outcome and na the number of them, while bj and nb stand for the corresponding values for the second outcome.

Brute force. The straightforward way to solve the problem is to simply try all possible subsets of bets. There are 22n of them, which is small enough for n10 and solves the first subproblem.

Greedy. Let's simplify the formula we're trying to maximize. If we subtract 1 from every quota, the formula simplifies to min(ainb,bjna). Note that the two outcomes are independent for a fixed na and nb — the exact set of odds we choose for the first outcome does not affect the optimal choice of bets for the second outcome. Therefore, it makes sense to sort the values ai and bi. We can choose the na largest ai and nb largest values bi for every pair of na and nb. This gives us an O(n2) solution for the second subproblem.

Ternary search. Consider what is the score for a fixed value of na and different values of nb. As we increase nb, the score grows until the term bjna becomes greater than ainb, at which point it starts to decrease. Therefore, we can use ternary search to find its maximum or a binary search over the list of differences in score for consecutive values of nb. Precompute prefix sums to compute the score for a given pair na and nb in O(1). Time complexity of this solution is O(nlogn).

Linear solution. There is in fact a linear solution assuming the values are already sorted. We can try to visualize what is going on as we try different values of na and nb. For every ai that we bet on, we increase the first value in the minimum by ai and decrease the other one by 1, while trying to maintain the lower one as large as possible. This would suggest that if nb is the optimal choice for na, then as we increase na to na+1, we should also increase nb or keep it at the same value but not decrease it.

Let A=naainb and B=nbbjna where nb is the optimal choice for na. As we increase na by 1, we get A=A+a and B=B1. We will handle two cases:

  1. AB. Because B is already the smaller of the two values, decreasing nb would only make the minimum smaller.
  2. A<B. Suppose that decreasing nb would give us a better solution. The new score in this case min(A+1,Bb) would have to be larger than the previous one min(A,B)=A. This would imply A<Bb. Rewriting it, we get A+1<Bba. This means that nb is not the optimal solution for na as we assumed, because we could decrease nb and obtain a better solution for na; we have a contradiction.

We don't have to restart the search for nb from scratch for every value of na in increasing order. Instead, we can simply increase nb until we find the maximum for a given na. For na+1, we start the search for nb from where we finished previously, overall making a single pass over all values of nb.


Comments

There are no comments at the moment.