Editorial for IOI '15 P5 - Sorting
Submitting an official solution before solving the problem yourself is a bannable offence.
(DMOJ curator's note: The flavourtext is completely different. I added this editorial in case it's still useful...)
Subtask 1
In subtask 1, Bob does not change sequence , thus any method that can sort the sequence in increasing order will be a solution.
A bubble sort or insertion sort may cost at most swaps. A merge sort or quick sort may cost at most
swaps.
The optimized solution is selection sort. Since the sequence contains exactly once, we can swap value
into position
one by one (
). This costs at most
swaps. It can be proved that the number of swaps is minimized.
Subtask 2
In subtask 2, Bob will swap positions and
, which makes the problem more complicated. However, the solution does not change much. We can swap value
into position
one by one from the rightmost position to the leftmost one (
). The number of swaps is also minimized.
Subtask 3
In subtask 3, we distinguish Alice and Bob's swaps. Let be
plates in a queue, and
be
apples on plates
. Then
.
Apple: 4 3 2 1 0
Plate: 0 1 2 3 4
If Bob swaps two positions and
, it can be viewed as two plates being swapped. For example, in the above case, if Bob swaps numbers at positions
and
, the result sequence will be:
Apple: 3 4 2 1 0
Plate: 1 0 2 3 4
If Alice swaps two positions and
, it can be viewed as two apples
and
are swapped. For example, if Alice swaps numbers at positions
and
, it can be viewed as apples
and
are swapped:
Apple: 0 4 2 1 3
Plate: 1 0 2 3 4
Now Bob swaps two plates in each round and Alice swaps two apples. Since they operate on different objects, the sequence of operations can be separated. Let be the number of rounds Alice takes. It can be viewed as Bob first swapping
pairs of plates, then Alice swapping
pairs of apples. Since Alice can sort all apples in increasing order in less than
swaps, the minimum number of rounds is less than
.
Solution: One solution for this problem would be to enumerate from
to
, and check whether Alice can sort all apples within
swaps after Bob swap first
pairs of plates. This costs
time and will get timeout for some test cases.
Binary Search: For this problem, if is a solution for Alice in
swaps, then
must be a solution for Alice in
swaps (
). Which means that if there is a feasible solution in
swaps, then there must be feasible solutions in more than
swaps. Which makes the problem solvable using binary search. The time complexity reduces to
and full mark is expected.
Implementation: The implementation of this problem may be a little bit error-prone. It is recommended that programs first find out pairs of numbers (i.e. pairs of apples) to swap, then transform numbers into their positions. An inversed array that maps each number to its position is helpful.
Comments
The time solution is essentially updating a set of cycles under splicing (edge insertion/deletion) operations. Some care is needed in test data making to distinguish it from
time solutions, perhaps the bounds can be raised to ease this process.
This solution can also be made to run in or
time using BBST-like data structures (e.g. http://contest.felk.cvut.cz/11cerc/solved/r/). This is way more work than the intended solution though.
Comments