Radar Installation |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB |
Total submit users: 562, Accepted users: 500 |
Problem 10023 : No special judgement |
Problem 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 21 2-3 12 11 20 20 0 |
Sample Output |
Case 1: 2Case 2: 1 |
Problem Source |
Bei jing 2002 |
解题思路:
radar installation
在海里有很多小岛,每个小岛位置由x,y坐标确定。而我们只能在海岸线上建雷达站,同时给定雷达站的最大覆盖距离d(以雷达站为圆心,d为半径),要求所有小岛都要被雷达站覆盖,求最少的雷达站数目,如果没有解决方案,输出-1。
贪心策略1:让某个雷达站覆盖尽量多的小岛?--》不正确
贪心策略2:从左往右建雷达站:最左边的雷达站需要覆盖最左边的小岛,同时位置尽量右靠。依次重复。 1. 如何确定雷达可能位置:以小岛为圆心,画圆,与海岸线的右交点为建雷达站点。 2. 对于所有小岛,求出左右交点,从左至右,以第一个右交点为基准, 如果下一海岛的左交点小于或等于该右交点,则此海岛能够被该雷达站覆盖, 不过这里要注意,如果出现该海岛的右交点小于或该右交点的情况,基准应该换成该海岛的右交点; 如果下一海岛的左交点大于该右交点,则需要新建雷达站,雷达站数++,同时以这一海岛的右交点为基准,重复上步骤;
代码(C++):
#include#include #include using namespace std;struct node{ double left,right;//以每个岛为圆心,与x轴的左右交点 }island[1001];//1001个实例 //sort的比较函数 bool cmp(node a,node b){ return a.left >n>>d && n!=0){ CaseNum++; flag=0; //输入,并记录island.left 和island.right for(int i=0;i >x>>y; //如果小岛到x轴的距离y大于d,则没有解决方案 if(y>d) { flag=1;//没有解决方案 } //记录以每个小岛为圆心,d为半径的圆与x轴的左右交点 island[i].left=x-sqrt(d*d-y*y); island[i].right=x+sqrt(d*d-y*y); } //没有解决方案 if(flag==1) { cout<<"Case "< <<": -1"< tmp){ count++; tmp=island[i].right; } else if(island[i].right