Cow Steeplechase
Time limit1sMemory limit128 MB
Choose as many of N axis-parallel segments as possible so that no two chosen segments share any point.
- Level
Medium7 of 10
- Topics
- Graph, Union-find, Geometry, Greedy
- Solved
- No attempts yet
Problem
Farmer John has a brilliant idea for the next great spectator sport: Cow Steeplechase! As everyone knows, regular steeplechase involves a group of horses that race around a course filled with obstacles they must jump over. FJ figures the same contest should work with highly-trained cows, as long as the obstacles are made short enough.
To design his course, FJ makes a diagram of all the () possible obstacles he could build. Each one is a line segment in the 2D plane that is parallel to the horizontal or the vertical axis. Obstacle has distinct endpoints and , with . An example layout is:
--+-------
-----+-----
---+--- |
| | |
--+-----+--+- |
| | | | |
| --+--+--+-+-
| | | |
|
FJ would like to build as many obstacles as possible, subject to the constraint that no two of them intersect. Starting from the diagram above, FJ can build 7 obstacles:
----------
-----------
------- |
| |
| | |
| | | |
| | | |
| | | |
|
Two segments intersect if they share any point in common, even an endpoint of one or both segments. You may assume that no two horizontal segments in the input intersect, and likewise no two vertical segments in the input intersect.
Determine the maximum number of obstacles FJ can build.
Input
- Line 1: A single integer .
- Lines 2 to : Line contains four space-separated integers describing obstacle : , , , and .
Output
- Line 1: The maximum number of pairwise non-intersecting segments FJ can choose.
Hint
In the sample there are three candidate obstacles: a horizontal segment from to , and two vertical segments, one from to and one from to . The horizontal segment crosses both vertical segments, so at most two obstacles can be built. Choosing the two vertical segments, which do not intersect each other, yields the optimal answer of 2.