#C3051. Maximum Non-overlapping Tasks

    ID: 46436 Type: Default 1000ms 256MiB

Maximum Non-overlapping Tasks

Maximum Non-overlapping Tasks

You are given n tasks. Each task is represented by its start and end times as a pair of integers. A person can only work on one task at a time, and a task can only be started at or after the end time of the previous task. Your goal is to determine the maximum number of tasks that can be completed.

The problem can be solved using a greedy algorithm. First, sort the tasks by their end times. Then, iterate over the tasks and select a task if its start time is not less than the end time of the previously selected task.

The selection criteria can be mathematically represented as follows:

$$\text{if } s_i \geq e_{prev} \text{ then select task } i.$$

Here, \(s_i\) is the start time of task \(i\) and \(e_{prev}\) is the end time of the last selected task.

inputFormat

The first line contains a single integer \(n\) representing the number of tasks.

The following \(n\) lines each contain two space-separated integers representing the start time and end time of a task.

It is guaranteed that \(1 \leq n \leq 10^5\) and the times are positive integers.

outputFormat

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

## sample
3
1 3
2 5
4 6
2