Printed Circuit Board
Time limit1sMemory limit128 MB
Given N wires each connecting a bottom-edge point to a top-edge point, find the minimum number of layers so that crossing wires never share a layer, which reduces to finding the maximum count of pairwise-crossing wires.
- Level
Medium5 of 10
- Topics
- Sorting, Binary search, Greedy
- Solved
- No attempts yet
Problem
On a printed circuit board, conductive wires are laid on a non-conductive base. Two conductors on the same layer cannot cross without causing a short circuit, so more complex boards use several conductive layers separated by non-conductive material. Because a board with more layers costs more, manufacturers try to assign the required conductors to layers so that as few layers as possible are used.
In this problem we consider boards on which every conductor connects two ports located on opposite edges of the board, and we want to minimize the number of layers.
Consider the board shown on the left of the figure below. If one conductor must connect A to B and another must connect D to C, both can be placed on a single layer, as shown in the middle. But a conductor connecting A to C together with one connecting D to B cannot share a layer, as shown on the right.

Write a program that, given the endpoints of the conductors on a board, determines the minimum number of layers needed to accommodate all of them.
You may assume that the width of a conductor is very small compared with the distances between ports; that is, between any two conductors there is always enough room for a third one.
Input
The first line contains (), the number of conductors. Each of the next lines contains two integers and (), separated by a space, meaning that the -th conductor must connect the points and . You may assume that all endpoints are distinct.
Output
Print a single integer: the minimum number of layers needed to accommodate all of the required conductors.