#C7198. Maximum Non-Overlapping Tasks

    ID: 51042 Type: Default 1000ms 256MiB

Maximum Non-Overlapping Tasks

Maximum Non-Overlapping Tasks

You are given a set of tasks, where each task is represented as a time interval (s, e), with s representing the start time and e representing the finish time.

Your goal is to select the maximum number of non-overlapping tasks such that each selected task completes before or exactly at a given timeline T. A task is considered valid if its finish time satisfies the condition: \( e \le T \).

The tasks are provided in an unordered manner. The optimal solution can be achieved with a greedy algorithm: sort the tasks by their end times and then iteratively select the next task that does not overlap with the previously selected one.

Input Format: The first line contains two integers \( n \) and \( T \), where \( n \) is the number of tasks. This is followed by \( n \) lines, each containing two integers representing the start time and finish time of one task.

Output Format: Output a single integer representing the maximum number of non-overlapping tasks that can be completed by time \( T \).

inputFormat

The input is read from standard input (stdin) and consists of:

  • The first line contains two space-separated integers \( n \) (the number of tasks) and \( T \) (the timeline by which tasks must be completed).
  • The following \( n \) lines each contain two space-separated integers \( s \) and \( e \), representing the start time and end time of a task.

It is guaranteed that tasks with \( e > T \) cannot be chosen.

outputFormat

The output should be written to standard output (stdout) and consist of a single integer: the maximum number of non-overlapping tasks that can be completed within the timeline \( T \).

## sample
4 10
1 3
2 5
4 6
6 9
3

</p>