## Editorial for CCO '23 P3 - Line Town

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.

Author: zhouzixiang2004

An initial observation is that the final happiness of a resident depends only on whether the resident's final position differs from their initial position by an odd or an even displacement. Therefore, the only important information to describe a state is a permutation representing where the residents end up. We can rephrase the problem as finding a permutation with the smallest number of inversions such that the array for is nondecreasing.

We will present the solution to subtasks 5 and 6 first as it helps put all the subtasks into a bigger picture.

Consider the element with the largest , say at index . We must move to either index or , and it is possible that only one or none of these indices result in having the correct sign in the end. After doing so, we can effectively ignore and continue this reasoning recursively on the remaining elements, except for the fact that where ends up might affect the parity of the final indices of the remaining residents.

This suggests using dynamic programming with a state of the form , representing the minimum number of swaps to put all the largest s at an endpoint of the line and with the correct sign, where the number of residents assigned to the left end is . Note that it suffices to consider greedily making swaps to move the residents into an endpoint in decreasing order of , as every inversion gets accounted for exactly once this way. Let be the number of residents left of the th largest with a higher and be the number of residents right of the th largest and with a higher . The transitions are of the form and , both only considered if the sign is correct. This dynamic programming can be performed in linear time after precalculating and .

Finding and naively takes time, which solves subtask 5, and a binary indexed tree similar to inversion counting can be used to find and in time, solving subtask 6.

#### Interlude

The path to the full solution is more clear after solving subtasks 5 and/or 6. We will still use dynamic programming with a similar state, except we need to consider all the residents with the same simultaneously, finding the minimum number of inversions incurred to move all of them to an endpoint while fixing the parity of how many residents are moved towards the left endpoint.

A solution to subtasks 3 and/or 4 will integrate nicely into a full solution, as we can view residents with the same as and residents with smaller in between them as . Subtasks 1 and 2 are easier versions of this task where s are not allowed.

Many different perspectives may lead to a solution, some of which are more obviously correct than others. One of the most direct perspectives (working with the original s) can lead to a solution resembling greedy bracket matching. We will present a different perspective that leads more easily to a subtask 3 and 4 solution.

Consider flipping the sign of every other . After this transformation, the swaps become normal swaps that do not flip any signs. There are candidates for the final configuration of s. For each final configuration, focus on the set of indices where in the initial and final states, say and . If these sets do not have the same number of elements, then this configuration is unreachable. Otherwise, a swap is equivalent to "shifting" an index by to the left or right, and it follows that the answer is .

For subtask 1, it suffices to implement the above idea naively in time. For subtask 2, note that only one value of changes each time as we sweep through the possible final configurations, so a running sum can be maintained to solve the problem in time. It may be necessary to consider some cases on whether the number of s in the initial state (after flipping every other ) is one more or less than .

Like in subtasks 1 and 2, we begin by flipping the sign of every other . There are candidates for the final configuration, where is the number of s among .

For subtask 3, we may consider all these candidates independently. For each configuration, we need to solve the following problem:

Given two length arrays of elements in , find the minimum number of swaps required to transform one array into the other.

This problem can be modelled using inversion counting: In both arrays, label the s with from left to right for some , the s with for some , and the s with . The answer is the number of inversions between these two permutations. Finding the number of inversions can be done in time using a BIT, for a total time complexity of (since there are only distinct elements in the arrays, it is also possible to do this in total).

For subtask 4, note that only a few elements change their inversion count each time as we sweep through the possible final configurations, so we can maintain a running sum instead to solve the problem in or time. Similar to subtask 2, it may be necessary to consider some cases on whether is odd and whether the number of s in the initial state is one more or less than .