Editorial for APIO '15 P3 - Palembang Bridges
Submitting an official solution before solving the problem yourself is a bannable offence.
Subtask 1
Ignore the paths which don't cross the river (i.e., compute them separately).
Also, ignore the length of the bridge (i.e., compute it separately).
It can be proved that the solution is optimal when the bridge is built at one of the positions or . Therefore, we can choose the position by brute force. The complexity will be .
Subtask 2
If we build a bridge at position , then (driving distance of citizen ) will be . The total sum is .
Sort in non-decreasing order, and let the sorted sequence be . Then, .
It can be proved that is minimized when . Then, will be .
The complexity of this solution is .
Subtask 3
Similar to Subtask 1, it can be proved that the solution is optimal when the bridge is built at two of the positions or . Therefore, we can choose the positions by brute force. The complexity will be .
Subtask 4
Suppose we build two bridges at positions and (wlog ).
For a particular citizen , let and . Note that we only consider citizen who has to cross the river to go from home to the office. now can be considered an interval.
For a given interval (again wlog ), how do we determine which bridge to use? We can only use bridge iff
(If they are equal, we can use either bridge).
Using Equation 1, we will now prove the following
Lemma 0.0.1. It is optimal to use the bridge at if
And it is optimal to use the bridge at if
Note that this does not imply the inverse. That is, optimality of using bridge at or does not imply the equations.
Proof. To prove Lemma 0.0.1, we need to prove that we can derive Equation 1 from Equation 3. We do so case by case. Denote the interval with
|---------|
Case 1
|---------| L R
That is, we have . In this case, Equation 1 expands to
Which is always true. So, we need to prove that in this case, always holds. Since we have :
Case 2
L R |---------|
That is, we have . We prove similar to Case 1. Equation 1 expands to
Which is always false. So, we need to prove that in this case, never holds. Since we have :
Case 3
|---------|
L R
Thus, we have . Equation 1 expands to
Which is always false. So, we need to prove that in this case, never holds:
Case 4
|---------|
L R
Thus, we have . Equation 1 expands to
Which is always true. So, we need to prove that in this case, always holds:
Case 5
|---------|
L R
Thus, we have . Equation 1 expands to
Which is always false, and in particular, it implies that the distance of using either of the bridges is the same. Thus, using any of the two bridges is always optimal.
Case 6
|---------|
L R
Thus, we have . From Equation 3:
Which is equation 1. □
Thus, there exists a way to separate the intervals into those which should go to the left bridge and those which should go to the right bridge. Sort the intervals by , and for every possible separation of this sequence of intervals, try to find the optimal placement of two bridges.
The optimal placement of a partition can be solved using the algorithm in Subtask 2. So, the total complexity is .
Subtask 5
One way to do this in : we keep track of a data structure to count the cost of placing a bridge in each possible position (initially, all are zero). We add the intervals one by one. When we add an interval , we are actually adding to each position , and . , though, is convex, so the sum over all these convex functions is a convex function. So we can ternary search over the sum of these convex functions to find the minimum in time.
There is another solution in . Note that the optimal location of a bridge in each prefix is between the medians of the positions. So, the problem reduces to finding medians of each prefix. This incremental medians problem can be solved by using two heaps: one min-heap, and one max-heap. When we add an interval, we add and in the heaps in such a way that both heaps contain equal number of elements (possibly transferring elements between heaps). Then, the medians can be found in .
Comments