Radar Installation
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.
Figure A Sample Input of Radar Installations
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.
Figure A Sample Input of Radar Installations
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
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 1: 2
Case 2: 1
题目大意(转):是为了求出能够覆盖所有岛屿的最小雷达数目,每个小岛对应x轴上的一个区间,在这个区间内的任何一个点放置雷达(在x轴上),则可以覆盖该小岛,区间范围的计算用[x-sqrt(d*d-y*y),x+sqrt(d*d-y*y)]。
这样,问题即转化为已知一定数量的区间,求最小数量的点,使得每个区间内斗至少存在一个点。每次迭代对于第一个区间, 选择最右边一个点, 因为它可以让较多区间得到满足,。如果不选择第一个区间最右一个点(而选择前面的点), 那么把它换成(前面区间)最右的点之后, 以前得到满足的区间, 现在仍然得到满足。
1 #include <iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cmath> 5 using namespace std; 6 7 #define maxn 1001 8 9 struct node 10 { 11 double left; 12 double right; 13 }island[maxn]; 14 15 bool cmp(node a,node b) 16 { 17 return a.left<b.left; 18 } 19 20 int main() 21 { 22 int i,n,k=1,count,flag; 23 double d,x,y,temp; 24 while(scanf("%d%lf",&n,&d) != EOF) 25 { 26 flag=0; 27 if(n==0&&d==0) 28 break; 29 for(i=0;i<n;i++) 30 { 31 scanf("%lf%lf",&x,&y); 32 if(y>d||d<0) 33 { 34 flag=1; 35 } 36 island[i].left=x-sqrt(d*d-y*y);//先算出每个岛的雷达覆盖区间 37 island[i].right=x+sqrt(d*d-y*y);//先算出每个岛的雷达覆盖区间 38 } 39 if(flag) 40 { 41 printf("Case %d: -1 ",k++); 42 continue; 43 } 44 sort(island,island+n,cmp);//按左端点排序 45 temp=island[0].right;//排序完成后,第一个雷达建立在第一个区间的右端点 46 count=1; 47 for(i=1;i<n;i++) 48 {//如果左端点在雷达左边,这个时候要考虑区间的右端在雷达的左边还是右边,如果是右边,那雷达位置就不变 49 if(island[i].right<temp)//如果在左边,那现在的雷达是覆盖不了的 50 { 51 temp=island[i].right;//所以要把雷达放在该区间的右端点,这样能同时覆盖原来的岛和现在的岛 52 } 53 else if(island[i].left>temp)//判断每个区间的左端点,如果在最新建立的雷达右边 54 { 55 count++;//那么肯定需要一个新雷达 56 temp=island[i].right;//而且也建在该区间右端 57 } 58 } 59 printf("Case %d: %d ",k++,count); 60 } 61 return 0; 62 }