#K11511. Maximum Number of Non-Overlapping Tasks

    ID: 23485 Type: Default 1000ms 256MiB

Maximum Number of Non-Overlapping Tasks

Maximum Number of Non-Overlapping Tasks

You are given a list of tasks, each characterized by a start time and an end time. Two tasks are considered non-overlapping if the start time of a task is not less than the end time of the previously selected task. Your goal is to determine the maximum number of non-overlapping tasks that can be scheduled.

Problem Details:

  • Each task is represented by a pair of integers (start, end).
  • You may assume that the tasks can occur at any time (the times could be negative, zero, or positive).
  • The optimal solution is achieved by selecting tasks in order of increasing end times, which is a greedy strategy.

The formal problem can be expressed as follows. Given a set of tasks\( \{(s_1,e_1), (s_2,e_2), \ldots, (s_n,e_n)\} \), find the maximum size subset \( S \) such that for any two tasks \((s_i,e_i)\) and \((s_j,e_j)\) in \(S\) with \(i \neq j\), the tasks do not overlap, i.e., \(s_j \ge e_i\) when arranged in the order of completion.

inputFormat

The input is read from stdin and is formatted as follows:

  1. The first line contains an integer n, representing the number of tasks.
  2. Each of the next n lines contains two space-separated integers representing the start time and end time of a task.

outputFormat

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

## sample
3
1 3
2 5
4 6
2

</p>