Editorial for IOI '02 P2 - Utopia Divided
Submitting an official solution before solving the problem yourself is a bannable offence.
The two dimensional problem can be solved by two separate one dimensional subproblems.
The one dimensional problem is: Given code numbers and a sequence of region signs (each of which is or ), produce a sequence of signed code values so that the sign of matches the region sign.
We do this by first sorting the input code numbers into increasing order, and then assigning alternating signs to them so that , though iff . The key observation is that if we have used a segment of the data, then , and the sign of the sum matches that of . If the next code is to reverse the sign, we simply use , which guarantees the invariant of the last sentence still holds. A similar argument guarantees that using retains the sign of the sum.
The next important issue is where to start. Recall that keeping the sign of the sum the same requires taking a new value from the right. So count the number of sign changes in the input sequence of region signs. If this value is , give the first input sign, thus determining the alternation. The output can now be produced in a fairly straightforward manner.
Hence there is an solution that can be coded (if not discovered) quite easily. It is also quite reasonable to solve the problem by backtracking. This leads to an acceptable solution on smaller test cases (up to 8, or with care to 9 or even 10).
Comments