Problem 672
Course Schedule III
Hard · 30 pts · Greedy, Heap, Sorting
Problem
There are N online courses. Course i takes DURATION[i] days and must be finished by day LASTDAY[i]. You take courses one at a time, starting on day 1, and cannot take two at once. Find the maximum number of courses you can complete. Sort the courses by their last day. Take each one in turn, keeping the chosen durations in a max-heap; whenever the total time exceeds the current deadline, drop the longest chosen course.
Input
The first line contains N. Each of the next N lines contains DURATION[i] and LASTDAY[i].
Output
Print the maximum number of courses.
Constraints
1 <= N <= 10000; 1 <= DURATION[i], LASTDAY[i] <= 10000
Example input
4 100 200 200 1300 1000 1250 2000 3200
Example output
3
Explanation
For courses (100,200), (200,1300), (1000,1250) and (2000,3200), take the first (days 1-100), the third (101-1100) and the second (1101-1300). The fourth would end on day 3300, too late, so the answer is 3.