貪心法
區間覆蓋
一個長為 , 寬為 的田地有 個灑水器, 給定每個灑水器的位置, 以及其噴灑半徑 , 試計算出能噴到整個田地灑水器的最小數量, 若無法完全覆蓋,則輸出 ,反之輸出最小數量。
Source: 題目傳送門
sprinklers are installed in a horizontal strip of grass meters long and meters wide.
Each sprinkler is installed at the horizontal center line of the strip.
For each sprinkler we are given its position as the distance from the left end of the center line and its radius of operation.
What is the minimim number of sprinklers to turn on in order to water the entire strip of grass?
Input consists of a number of cases.
The first line for each case contains integer numbers , and with .
The next lines contain two integers giving the position of a sprinkler and its radius of operation. (The picture above illustrates the first case from the sample input.)
For each test case output the minimum number of sprinklers needed to water the entire strip of grass. If it is impossible to water the entire strip output ‘-1’.
8 20 2
5 3
4 1
1 2
7 2
10 2
13 3
16 2
19 4
3 10 1
3 5
9 3
6 1
3 10 1
5 3
1 1
9 1
6
2
-1