Editorial for IOI '09 Practice Task 3 - Museum
Submitting an official solution before solving the problem yourself is a bannable offence.
To solve this problem, we start by observing that if we have three vases with heights , and , such that either is odd and even, or even and odd, then no matter what is these three vases will not violate the condition of the exhibition organizers. This is because must therefore be odd, and so is not divisible by two, meaning that it is impossible for to be the average of and .
We therefore start by arranging the vases such that we place all the even vases first, and all the odd vases second. This gives an arrangement that looks like this:
where are the even heights in some order, and are the odd heights in some order.
Now consider any heights , , which violate the organizers' requirements. By the observations above, either and are both even (in which case is even, since it appears between and and all the even values are grouped together), or and are both odd (in which case is also odd). In other words, we can consider the problems of ordering the even numbers and the odd numbers separately.
Now suppose that is a permutation of the heights which satisfies the organizers' requirements (this is a smaller instance of the problem, so it can be solved by divide-and-conquer). Then simply assigning will satisfy the requirements on the even values. Similarly, given a permutation of the heights which satisfies the requirements, we can assign .
Examining the properties of the resulting sequence gives another approach to generating the same solution. We can write all the heights in binary form, and then sort them according to the reverse of their binary form. This sorts first on the least significant bit (i.e., on whether they are odd or even), then the next least significant bit and so on. To prove that this solution is valid, note that if is the average of and , then at the least significant bit that and first differ, and must have the same value for that bit, placing and in a separate group from when sorting on that bit.
Comments