Editorial for Baltic OI '18 P4 - Alternating Current
Submitting an official solution before solving the problem yourself is a bannable offence.
Subtask 1
There are only wirings, so when we can simply try all of them and check if any wiring satisfies the constraints. To check a wiring, we can go through all wires in each direction separately and for each wire mark the segments that are covered by that wire. This yields an algorithm which runs in time .
Subtask 4
No wire passes through the separator between segments and , so there is a natural order on wires, namely by starting point. We'll sort the wires according to this measure, and go through them one by one and assign their directions greedily.
When we see a wire, the choice is between assigning it clockwise and counter-clockwise direction. If the maximum ending point assigned to clockwise wires is greater than the maximum ending point assigned to counter-clockwise wires, then we are strictly better off assigning it a counter-clockwise direction, since clockwise and counter-clockwise directions are symmetrical. (Note that this symmetry comes from our common starting point.) Otherwise, we may assign it a clockwise wire. If we get any gaps, the answer is impossible, otherwise this greedy solution will work.
Subtask 5
The main observation is that if one wire covers only a subset of the segments covered by another wire , then we can without loss of generality assign the direction of to be opposite the direction of , because assigning the direction of to be the same as the direction of would accomplish nothing. Let us therefore assign parents to as many of the wires as possible, in such a way that:
- If is the parent of then covers a subset of the segments covered by .
- A wire which is itself a parent has no parent.
By doing this we have partitioned the wires into two disjoint sets: The set of parents and the set of nodes that have a parent. From now on we will focus on the set of parents, which we denote by .
No wire in covers only a subset of the segments covered by any other wire in , because if that were the case we could have let be the parent of . Instead, the wires in partially overlap or are completely disjoint.
Let us now sort the wires in by increasing start segment, and let be the sorted list of wires in . Let us now prove the following lemma:
Lemma 1. Suppose that is even and that there is a valid wiring. Then we get a valid wiring by assigning the wires in one direction and the wires in the other direction.
Proof. Suppose, for the sake of contradiction, that there is some segment which is not covered in both directions when the wires are assigned in one direction and the wires are assigned in the other. Since there exists a valid wiring, all segments must be covered by at least one wire in , so let us assume that covers . If or covered then would be covered by wires in both directions, and therefore must be the only wire in that covers . This means that all other wires (not in ) covering must be children of , but then we would assign their directions to be opposite the direction of , so is again covered by wires in both directions. □
Lemma 1 implies that in the case when is even, we can simply use an alternating assignment to the wires and then check whether that assignment is a valid one.
The case when is odd is slightly harder. For this case we use the following lemma:
Lemma 2. Suppose that is odd and that there is a valid wiring. Then there is an such that we get a valid wiring if we assign the wires in one direction and the wires in the other direction, where all indices are taken modulo .
Proof. Consider a valid wiring. Since is odd there exists an such that the valid wiring assigns the same direction to and . We claim that we get a valid wiring if we assign the wires in one direction and the wires in the other direction. Suppose for the sake of contradiction that there is some segment which is not covered in both directions by this wiring. Since there exists a valid wiring, must be covered by at least one wire in .
Suppose that is covered by or . If is also covered by or then is covered by wires in both directions, so we can assume that is not covered by or . This means that all other wires containing (not in ) must be children of either or , but then we would assign their directions to be opposite the directions we assign to and , so is again covered by wires in both directions.
Suppose that is covered by some wire , for . If or also covered then would be covered by wires in both directions, so let's assume that is the only wire in that covers . Then all other wires covering must be children of , which means that they are assigned directions opposite to the direction assigned to , so is covered by wires in both directions. □
For subtask 3 it is sufficient to iterate through all possible values of and check the resulting wiring for each. For subtask 5 we need to be more clever. A wiring that assigns the wires in one direction and the wires in the other direction is valid if and only if:
- All segments are covered by at least one wire.
- For all , the wires and all their children together cover, in both directions, all the segments covered by .
- The wires and all their children together cover, in both directions, all the segments covered by and .
The requirements 1 and 2 above are almost independent of the choice of (except that we require ), so when we change the value of we only need to check if requirement 3 is satisfied, which can be done in amortized constant time over all choices of .
The time complexity of the algorithm is dominated by the process of finding parents of nodes and sorting the wires in , which takes time .
Comments