Editorial for CEOI '17 P2 - Sure Bet
Submitting an official solution before solving the problem yourself is a bannable offence.
The problem is asking us to maximize the value , where represents the odds for placed bets on the first outcome and the number of them, while and 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 of them, which is small enough for and solves the first subproblem.
Greedy. Let's simplify the formula we're trying to maximize. If we subtract from every quota, the formula simplifies to . Note that the two outcomes are independent for a fixed and — 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 and . We can choose the largest and largest values for every pair of and . This gives us an solution for the second subproblem.
Ternary search. Consider what is the score for a fixed value of and different values of . As we increase , the score grows until the term becomes greater than , 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 . Precompute prefix sums to compute the score for a given pair and in . Time complexity of this solution is .
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 and . For every that we bet on, we increase the first value in the minimum by and decrease the other one by , while trying to maintain the lower one as large as possible. This would suggest that if is the optimal choice for , then as we increase to , we should also increase or keep it at the same value but not decrease it.
Let and where is the optimal choice for . As we increase by , we get and . We will handle two cases:
- . Because is already the smaller of the two values, decreasing would only make the minimum smaller.
- . Suppose that decreasing would give us a better solution. The new score in this case would have to be larger than the previous one . This would imply . Rewriting it, we get . This means that is not the optimal solution for as we assumed, because we could decrease and obtain a better solution for ; we have a contradiction.
We don't have to restart the search for from scratch for every value of in increasing order. Instead, we can simply increase until we find the maximum for a given . For , we start the search for from where we finished previously, overall making a single pass over all values of .
Comments