Editorial for CEOI '22 P5 - Measures


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.

Prepared by: Ivan Paljak, Paula Vidas, and Dominik Fistrić

Consider the situation when there are N people standing at coordinates a1,,aN, and without loss of generality let the coordinates be sorted, i.e. a1aN.

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 ti,j=12((ji)D(ajai)), for any ij, and let t=maxijti,j. We claim that topt is equal to t.

Clearly, t is a lower bound. After t seconds, the distance between people i and j can be at most their initial distance ajai plus 2t. Thus ajai+2topt(ji)D, which implies topt12((ji)D(ajai))=ti,j.

Now we prove t is an upper bound. Consider the following greedy strategy. Let b1,,bN be the final coordinates, calculated as: b1=a1t, and bi=max(ait,bi1+D) for i>1. First, we prove that nobody moved more than t. For some person j, let ij be the maximal index such that bi=ait. Then, we can see that bj=bi+(ji)D, so bjaj=(ji)D+biaj=(ji)D+aitaj0. On the other hand, bjajt, i.e. bjajt, so we're done. Second, we prove that after the rearrangement, no two consecutive people are standing closer than D. This follows from bibi1+D, and the proof is complete.

Naively calculating all ti,j and taking the maximum one, for each of the M scenarios, is enough to solve the first subtask. Time complexity is O(M(N+M)2).

To solve the second subtask, we don't need the formula. Instead, we can binary search topt directly. To check if some t is feasible, we use the greedy strategy from the proof above, and at the end check if all consecutive distances are at least D. Time complexity is O(M(N+M)logT), where T is the maximum possible value of topt given the constraints.

The third subtask can be solved using the formula for t. We can rewrite ti,j=12((aiiD)(ajjD)). Since each new person is added to the end of the line, it's enough to maintain the current maximum value of aiiD. Then, when jth person is added, it's easy to find the maximal ti,j over all i. Time complexity is O(N+M).

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 aiiD. 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 D for everyone on the right side of this person. Furthermore, values ti,j remain the same between people who are on the same sides relative to the new person, and it increases by D between people on opposite sides. Therefore, to find the new topt, it's enough to find the maximum of aiiD 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 O((N+M)log(N+M)).


Comments

There are no comments at the moment.