#P1712. [NOI2016] 区间
[NOI2016] 区间
Description
On the number line, there are closed intervals numbered from to . The -th closed interval is .
Now we need to choose intervals such that these intervals share at least one position. In other words, there exists an such that for every selected interval , we have .
For a valid selection, its cost is the length of the longest selected interval minus the length of the shortest selected interval.
The length of an interval is defined as , i.e., the value of the right endpoint minus the value of the left endpoint.
Find the minimum cost among all valid selections. If no valid selection exists, output .
Input Format
The first line contains two integers, representing and .
From line to line , each line contains two integers representing an interval. On line , the integers denote the left and right endpoints of the -th interval.
Output Format
Output a single integer on one line representing the answer.
6 3
3 5
1 2
3 4
2 2
1 5
1 4
2
Hint
Explanation for Sample Input/Output 1

As shown, when , , the minimum-cost plan is to select the three intervals . They all contain position , so the selection is valid. Among them, the longest interval is , and the shortest interval is , so the cost is .
Constraints and Conventions
::cute-table{tuack}
There are 20 test points in total. See the table below for details. | Test point ID | | | | |:-:|:-:|:-:|:-:| | 1 | | | | | 2 | ^ | | | | 3 | | | | | 4 | | ^ | ^ | | 5 | | | ^ | | 6 | | ^ | ^ | | 7 | | | | | 8 | | | ^ | | 9 | ^ | ^ | | | 10 | | | | | 11 | | | ^ | | 12 | ^ | | | | 13 | | | | | 14 | | | ^ | | 15 | | | ^ | | 16 | | | ^ | | 17 | | ^ | | | 18 | | | ^ | | 19 | | | ^ | | 20 | | | ^ |
For all test points, it is guaranteed that , , , .
Translated by ChatGPT 5
京公网安备 11011102002149号