#K36727. Maximum Non-Overlapping Tasks

    ID: 25819 Type: Default 1000ms 256MiB

Maximum Non-Overlapping Tasks

Maximum Non-Overlapping Tasks

In this problem, you are given a list of tasks, where each task is represented by its start time and end time. Your goal is to select the maximum number of tasks that can be performed without overlapping. A task ( (a, b) ) is said to be non-overlapping with another task ( (c, d) ) if ( a \geq d ) or ( c \geq b ). The optimal solution can be achieved by following a greedy strategy that sorts the tasks by their end times and then iteratively selects the task that finishes the earliest, ensuring that it does not conflict with the previously selected tasks.

Example:
For tasks: (1, 3), (2, 5), (4, 6), the maximum number of non-overlapping tasks is 2.

inputFormat

The input is given via standard input. The first line contains a single integer ( n ) representing the number of tasks. The following ( n ) lines each contain two space-separated integers ( a ) and ( b ), where ( a ) is the start time and ( b ) is the end time of the task.

outputFormat

Print a single integer to standard output representing the maximum number of non-overlapping tasks that can be scheduled.## sample

3
1 3
2 5
4 6
2