#P6281. [USACO20OPEN] Social Distancing S

[USACO20OPEN] Social Distancing S

Description

Due to the outbreak of the highly contagious cow disease COWVID-19, Farmer John is very worried about the health of his cows.

To limit the spread of the disease, Farmer John’s NN cows (2N1052\le N\le 10^5) decide to practice “social distancing” and spread out across the farm. The farm can be seen as a one-dimensional number line with MM disjoint intervals (1M1051\le M\le 10^5) that contain grass available for grazing. The cows want to stand at different integer positions, where each chosen position has grass, and maximize the value of DD, where DD is the distance between the closest two cows. Please help the cows find the maximum possible value of DD.

Input Format

The first line contains NN and MM. The following MM lines each contain two integers aa and bb describing an interval, where 0ab10180\le a\le b\le 10^{18}. No two intervals overlap or touch at endpoints. A cow standing on an endpoint of an interval is considered to be standing on grass.

Output Format

Output the maximum possible value of DD such that the distance between every pair of cows is at least DD. It is guaranteed that a solution with D>0D>0 exists.

5 3
0 2
4 7
9 9
2

Hint

Sample Explanation

One way to achieve D=2D=2 is to place the cows at positions 00, 22, 44, 66, and 99.

Subtasks

  • Test cases 22-33 satisfy b105b\le 10^5.
  • Test cases 44-1010 have no additional constraints.

Translated by ChatGPT 5