#K78677. Merging Overlapping Tasks
Merging Overlapping Tasks
Merging Overlapping Tasks
You are given a list of tasks, each represented as a pair of integers [start, end]. Some tasks may overlap or touch. Your goal is to merge all overlapping or touching tasks and output the resulting list of merged tasks in sorted order.
The merging process should combine any tasks that overlap or touch (i.e. if one task's end is at least the next task's start) into a single task spanning from the smallest start to the largest end value among them.
For example, if you have tasks (1, 3) and (2, 4), then the merged task is (1, 4). Additionally, tasks (1, 2) and (2, 3) should be merged into (1, 3).
Note: Use the LaTeX format for any mathematical expressions. For instance, if you need to denote the task interval, you can write it as \( (a, b) \).
inputFormat
The input is read from standard input (stdin) and is formatted as follows:
- 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\), representing a task that starts at \(a\) and ends at \(b\).
outputFormat
Output the merged tasks to standard output (stdout) in the following format:
- The first line should contain a single integer \(m\), the number of merged tasks.
- The next \(m\) lines each contain two space-separated integers representing the start and end of each merged task, in sorted order.
3
1 2
3 4
5 6
3
1 2
3 4
5 6
</p>