Description
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d.We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates.
Input
The input consists of several test cases. The first line of each case contains two integers n (1 n 1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases.
The input is terminated by a line containing pair of zeros.
Output
For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.
Sample Input
3 2
1 2
-3 1
2 1
1 2
0 2
0 0
Sample Output
Case 2: 1
题目大意: x轴上方是海,下面是陆地,海面上给出若干个小岛,现在给出灯塔可见半径d和每个灯塔的坐标,要求在陆地上选择建最少的灯塔保证每个岛都能可见,无解输出-1;
分析:首先如果无解,那么一定存在某个灯塔的坐标y大于灯塔半径k,直接输出-1;
如果有解,那么每个灯塔在海岸线上一定存在一个灯塔的可建范围l,r;
此时我们只需要从这些区间当中找到最少灯塔即可。
那么问题来了,如何找呢?
|
|
|
贪心!
我们将每个小岛按照右范围r从小到大排好,从最左边的岛开始,只要接下来的岛的左范围l小于当前岛的r,那么这两个岛一定可以共用一个灯塔,一直到范围r外的小岛,那么此时肯定需要建立其他灯塔才能满足解。
代码如下:
<script src="https://code.csdn.net/snippets/741681.js"></script>