AmazonOA #52
Minimum Sets (Adding One Delivery Zone)
估计中等可在线判题
Given some existing closed intervals, add exactly one closed interval [a,b] with 0≤b−a≤k. Intervals merge when they intersect or share an endpoint; integer endpoints that differ by 1 without a common point do not count as touching. Find the minimum number of connected blocks.
输入格式
The first line contains n k (1≤n≤200000, 1≤k≤10⁹), followed by n lines of a b (1≤a≤b≤10⁹). The endpoints of the new interval can be any integers you choose.
输出格式
Output a single integer answer.
样例 1
输入:
5 2
1 5
2 4
6 6
7 14
16 19
输出:
2
样例 2
输入:
1 1
1 2
输出:
1
样例 3
输入:
2 4
1 1
5 5
输出:
1
样例说明
Example 1: there are 4 blocks after merging; adding [5,7] of length 2 connects the first three blocks, leaving 2. Example 2: the new interval can be placed inside an existing interval, so there is still 1 block. Example 3: adding [1,5] of length 4 connects the two points into 1 block.