#P1204. Longest Milking and Idle Intervals

    ID: 14145 Type: Default 1000ms 256MiB

Longest Milking and Idle Intervals

Longest Milking and Idle Intervals

Three farmers wake up at 5 AM every day and go to the cowshed to milk their cows. Each farmer works during a specific time interval (in seconds from 5 AM). For example, consider the following three intervals:

  • Farmer 1: from 300 seconds to 1000 seconds
  • Farmer 2: from 700 seconds to 1200 seconds
  • Farmer 3: from 1500 seconds to 2100 seconds

In this example, the longest continuous period with at least one farmer milking is 900 seconds (from 300 to 1200 seconds) and the longest continuous idle period (with no one milking, calculated within the span of the first start to the last end) is 300 seconds (from 1200 to 1500 seconds).


Your task is to write a program that reads a list of n intervals (each interval representing one farmer milking one cow) and computes:

  1. The length of the longest continuous time interval during which at least one farmer is milking.
  2. The length of the longest continuous time interval during which no one is milking (considering only the period from when milking starts to when milking ends).

Note: If the end time of one interval is equal to the start time of the next, they are considered continuous (i.e. they merge together).

inputFormat

The first line contains an integer n representing the number of farmers (and cows). Each of the next n lines contains two integers s and e, which represent the start and end times (in seconds from 5 AM) of a farmer's milking session.

outputFormat

Output two integers separated by a space: the longest continuous milking duration and the longest idle duration (with no milking) within the overall time span.

sample

3
300 1000
700 1200
1500 2100
900 300