Editorial for CEOI '22 P5 - Measures
Submitting an official solution before solving the problem yourself is a bannable offence.
Prepared by: Ivan Paljak, Paula Vidas, and Dominik Fistrić
Consider the situation when there are people standing at coordinates , and without loss of generality let the coordinates be sorted, i.e. .
It's easy to see that there exists an optimal rearrangement after which the order of the people remains the same as the initial order. Otherwise, if there is a moment when some two people swap places in the order, the total time won't increase if they instead don't change their relative order at that moment (but instead move where the other person would have moved).
Let , for any , and let . We claim that is equal to .
Clearly, is a lower bound. After seconds, the distance between people and can be at most their initial distance plus . Thus , which implies .
Now we prove is an upper bound. Consider the following greedy strategy. Let be the final coordinates, calculated as: , and for . First, we prove that nobody moved more than . For some person , let be the maximal index such that . Then, we can see that , so . On the other hand, , i.e. , so we're done. Second, we prove that after the rearrangement, no two consecutive people are standing closer than . This follows from , and the proof is complete.
Naively calculating all and taking the maximum one, for each of the scenarios, is enough to solve the first subtask. Time complexity is .
To solve the second subtask, we don't need the formula. Instead, we can binary search directly. To check if some is feasible, we use the greedy strategy from the proof above, and at the end check if all consecutive distances are at least . Time complexity is , where is the maximum possible value of given the constraints.
The third subtask can be solved using the formula for . We can rewrite . Since each new person is added to the end of the line, it's enough to maintain the current maximum value of . Then, when person is added, it's easy to find the maximal over all . Time complexity is .
In order to solve the fourth subtask, we need to be able to insert a person at an arbitrary place in the line. Consider the value . When a person is inserted somewhere in the middle of the line, this value remains the same for everyone on the left side, and decreases by for everyone on the right side of this person. Furthermore, values remain the same between people who are on the same sides relative to the new person, and it increases by between people on opposite sides. Therefore, to find the new , it's enough to find the maximum of in the left part and the minimum in the right part.
This process can be simulated using a segment tree which supports adding a number to a range and querying for minimum and maximum of a range. This is a classic problem, for more info check this link. Time complexity is .
Comments