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 p1,,pN with the smallest number of inversions such that the array hi:=(1)|ipi|hpi for 1iN is nondecreasing.

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

Subtasks 5 and 6

Consider the element with the largest |hi|, say at index j. We must move hj to either index 1 or N, and it is possible that only one or none of these indices result in hj having the correct sign in the end. After doing so, we can effectively ignore hj and continue this reasoning recursively on the remaining elements, except for the fact that where hj 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 dp[k][b], representing the minimum number of swaps to put all the k largest |hi|s at an endpoint of the line and with the correct sign, where the number of residents assigned to the left end is b(mod2). Note that it suffices to consider greedily making swaps to move the residents into an endpoint in decreasing order of |hi|, as every inversion gets accounted for exactly once this way. Let lk be the number of residents left of the kth largest |hi| with a higher |hi| and rk be the number of residents right of the kth largest |hi| and with a higher |hi|. The transitions are of the form dp[k][b]+lkdp[k+1][b1] and dp[k][b]+rkdp[k+1][b], both only considered if the sign is correct. This dynamic programming can be performed in linear time after precalculating lk and rk.

Finding lk and rk naively takes O(N2) time, which solves subtask 5, and a binary indexed tree similar to inversion counting can be used to find lk and rk in O(NlogN) 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 |hi| 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 |hi| as ±1 and residents with smaller |hi| in between them as 0. Subtasks 1 and 2 are easier versions of this task where 0s are not allowed.

Subtasks 1 and 2

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 his) 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 hi. After this transformation, the swaps become normal swaps that do not flip any signs. There are N+1 candidates for the final configuration of his. For each final configuration, focus on the set of indices where hi=1 in the initial and final states, say a and b. 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 1 to the left or right, and it follows that the answer is |a1b1|+|a2b2|+.

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

Subtasks 3 and 4

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

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

Given two length N arrays of elements in {1,0,1}, 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 1s with 1,2,,x from left to right for some x, the 0s with x+1,,y for some y, and the 1s with y+1,,N. The answer is the number of inversions between these two permutations. Finding the number of inversions can be done in O(NlogN) time using a BIT, for a total time complexity of O(N2logN) (since there are only 3 distinct elements in the arrays, it is also possible to do this in O(N2) 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 O(NlogN) or O(N) time. Similar to subtask 2, it may be necessary to consider some cases on whether z is odd and whether the number of 1s in the initial state is one more or less than (Nz)/2.

Subtasks 7 and 8

To solve subtasks 7 and 8, we can combine the solution for subtasks 3 and 4 with the dynamic programming of subtasks 5 and 6.


Comments

There are no comments at the moment.