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
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
题目大意(转):是为了求出能够覆盖所有岛屿的最小雷达数目,每个小岛对应x轴上的一个区间,在这个区间内的任何一个点放置雷达(在x轴上),则可以覆盖该小岛,区间范围的计算用[x-sqrt(d*d-y*y),x+sqrt(d*d-y*y)]。
这样,问题即转化为已知一定数量的区间,求最小数量的点,使得每个区间内斗至少存在一个点。每次迭代对于第一个区间, 选择最右边一个点, 因为它可以让较多区间得到满足,。如果不选择第一个区间最右一个点(而选择前面的点), 那么把它换成(前面区间)最右的点之后, 以前得到满足的区间, 现在仍然得到满足。
1 #include2 #include 3 #include 4 #include 5 using namespace std; 6 7 #define maxn 1001 8 9 struct node10 {11 double left;12 double right;13 }island[maxn];14 15 bool cmp(node a,node b)16 {17 return a.left 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\n",k++);42 continue;43 }44 sort(island,island+n,cmp);//按左端点排序45 temp=island[0].right;//排序完成后,第一个雷达建立在第一个区间的右端点46 count=1;47 for(i=1;i temp)//判断每个区间的左端点,如果在最新建立的雷达右边54 {55 count++;//那么肯定需要一个新雷达56 temp=island[i].right;//而且也建在该区间右端57 }58 }59 printf("Case %d: %d\n",k++,count);60 }61 return 0;62 }