Hide

Problem V
Interval Scheduling

/problems/intervalscheduling/file/statement/en/img-0001.png
A set of 8 intervals corresponding to sample input 2. The maximum number of nonoverlapping intervals is 3, achieved by the highlighted intervals.

Consider a set of n intervals I1,,In, each given as an integer tuple (si,fi) with si<fi, such that the ith interval starts at si and ends at fi. We want to determine the maximum number of nonoverlapping intervals. More formally, we want to determine the size of a largest subset S{1,,n} such that for i,jS with ij we have fjsi or fisj. Note that the intervals [1,2] and [2,3] are not considered to be overlapping.

Input

The first line of input consists of an integer n{1,,105}, the number of intervals. Each of the following n lines consists of two space-separated integers s and f, indicating an interval starting at s and ending at f, with 0s<f109.

Output

Output a single integer, the largest number of nonoverlapping intervals.

Sample Input 1 Sample Output 1
3
1 4
2 8
5 9
2
Sample Input 2 Sample Output 2
8
0 6
1 4
3 5
3 8
4 7
5 9
6 10
8 11
3
Hide

Please log in to submit a solution to this problem

Log in