Problem 669
Maximum Number of Events That Can Be Attended
Medium · 20 pts · Greedy, Heap, Sorting
Problem
Event i can be attended on any single day from START[i] to END[i] inclusive. You can attend at most one event per day, and attending an event takes just that one day. Find the maximum number of events you can attend. Sweep the days in order: add every event that starts today to a min-heap of end days, throw away events that have already ended, and attend the one that ends soonest.
Input
The first line contains N. Each of the next N lines contains START and END of one event.
Output
Print the maximum number of events you can attend.
Constraints
1 <= N <= 100000; 1 <= START <= END <= 100000
Example input
3 1 2 2 3 3 4
Example output
3
Explanation
With events [1,2], [2,3] and [3,4], attend the first on day 1, the second on day 2 and the third on day 3. All 3 events fit.