Non-intersecting Segments

3.8

33 votes
Algorithms, Dynamic Programming, Easy
Problem

There are two different parallel lines L1 and L2 and there are N segments connecting them. Each segment connects two points (xi,yi) and (xi,yi) from these lines. You can assume that all the points (xi,yi) belong to L1 and all the points (xi,yi) belong to L2.
Your task is to find a subset of these segments such that there won’t be any two segments crossing each other. It is guaranteed that all the given points have distinct coordinates.

Input Format
The first line contains an integer N, the number of segments (1N105)
Each of the next N lines contain 4 space-separated integers xi,yi,xi,yi - each describing the endpoints of a segment (109xi,yi,xi,yi109)

Output Format
Print the maximum number of segments such that there won’t be any two of them crossing each other.

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

All these three segments intersect each other, so we cannot choose any pair from them.

Editor Image

?