Sprinklers
Xem dạng PDF
Gửi bài giải
Điểm:
1,00 (OI)
Giới hạn thời gian:
1.0s
Giới hạn bộ nhớ:
256M
Input:
stdin
Output:
stdout
Dạng bài
Ngôn ngữ cho phép
C, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch
You are given a straight garden of length ~L~, represented by the interval ~[0, L]~.
There are ~N~ sprinklers. The ~i~-th sprinkler is installed at position ~x_i~ and can water all points from ~x_i - r_i~ to ~x_i + r_i~.
You want to turn on as few sprinklers as possible so that every point of the garden is watered.
If it is impossible to water the whole garden, print ~-1~.
Input Format
The first line contains two integers ~N~ and ~L~.
Each of the next ~N~ lines contains two integers ~x_i~ and ~r_i~:
- ~x_i~: the position of the sprinkler
- ~r_i~: its watering radius
Output Format
Print a single integer:
- the minimum number of sprinklers needed to cover the whole garden, or
- ~-1~ if full coverage is impossible
Example
Input
5 10
1 2
3 3
6 2
8 3
10 1
Output
3
Explanation
One optimal choice is to turn on the sprinklers at:
- ~x = 3, r = 3~, covering ~[0, 6]~
- ~x = 6, r = 2~, covering ~[4, 8]~
- ~x = 8, r = 3~, covering ~[5, 11]~
Together they cover the entire garden ~[0, 10]~.
Constraints
- ~1 ≤ N ≤ 200000~
- ~1 ≤ L ≤ 10^9~
- ~0 ≤ x_i ≤ L~
- ~0 ≤ r_i ≤ 10^9~
Subtasks
50%of the tests satisfy ~N ≤ 2000~.50%of the tests use the full constraints.
Bình luận