#K71362. Maximum Number of Non-Overlapping Tasks

    ID: 33515 Type: Default 1000ms 256MiB

Maximum Number of Non-Overlapping Tasks

Maximum Number of Non-Overlapping Tasks

You are given a set of tasks, where each task is described by an id, a start time, and an end time. Your goal is to determine the maximum number of non-overlapping tasks that can be performed. A task is considered non-overlapping if its start time is not earlier than the end time of the previously selected task. The optimal solution is to use a greedy algorithm that sorts the tasks by their finish times. Mathematically, if tasks are sorted by end time, then for a task i to be scheduled after a task j, it must satisfy (start_i \geq lastEnd).

Read the input from standard input (stdin) and output the result to standard output (stdout).

inputFormat

The first line contains a single integer (n) representing the number of tasks. Each of the following (n) lines contains three space-separated integers: the task id, the starting time, and the ending time of the task.

outputFormat

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

4
1 1 4
2 2 6
3 4 7
4 5 8
2