#C5819. Maximum Non-overlapping Tasks

    ID: 49510 Type: Default 1000ms 256MiB

Maximum Non-overlapping Tasks

Maximum Non-overlapping Tasks

You are given a set of tasks, each represented by a pair of integers ( (s, e) ), where ( s ) is the start time and ( e ) is the end time of the task. Your goal is to select the maximum number of tasks that do not overlap. In other words, after selecting a task, any subsequent task chosen must have a start time that is not less than the end time of the previously selected task.

This problem can be solved using a greedy algorithm which first sorts tasks by their finishing times and then iteratively selects the next task whose start time is at least the end time of the last selected task.

inputFormat

The input begins with an integer ( n ) on the first line, representing the number of tasks. Each of the following ( n ) lines contains two space-separated integers denoting the start time and end time of a task.

outputFormat

Output a single integer representing the maximum number of non-overlapping tasks that can be completed.## sample

0
0