Open Intervals
InterviewTime limit1sMemory limit128 MB
Given up to 50 open intervals per test case, pick the largest subset where no two intervals overlap, counting touching endpoints as compatible.
- Level
Medium4 of 10
- Topics
- Greedy, Sorting, Intervals, Implementation
- Solved
- No attempts yet
Problem
You are given open intervals on the number line. Each interval represents the start and end time of some activity that needs the same resource. Choose as many intervals as possible so that no two chosen intervals overlap, and report the maximum number of intervals you can choose.
Because the intervals are open, two intervals that meet only at an endpoint — for example and — are considered non-overlapping, so both of them may be chosen.
Input
The input consists of several test cases. The first line of each test case contains a positive integer (), the number of intervals. Each of the next lines contains two positive integers separated by one or more blanks, describing one interval. The end of the input is marked by a line containing a single .
Output
For each test case, print on its own line the largest number of intervals that can be chosen so that no two of them overlap.