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.
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
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轴上可以放置雷达(放置位置可为小数),以放置位置为圆心,d为半径做圆来覆盖岛屿,输出最小的雷达数以覆盖所有的岛屿。无法覆盖输出-1。
解题思路:假设某个岛屿的坐标为(x,y),则在(x-sqrt(d*d-y*y),x+sqrt(d*d-y*y))范围内的雷达可以覆盖到此岛屿。
先求出各个岛屿的可覆盖雷达范围,在根据其区间左端点进行排序,再通过贪心确定需要的最少雷达数。
ps:当一个岛屿的纵坐标大于d时,不可能覆盖到,输出-1。
ps2:进行贪心时,设置一个变量tmpr=p[1].r。
当p[i].r<tmpr时,tmpr=p[i].r 否则p[i]点会漏掉。
当p[i].l>tmpr是,tmpr=p[i].r,cnt++
Code:
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 using namespace std; 5 struct point 6 { 7 double x,y; 8 double l,r; 9 } p[100000]; 10 bool cmp(struct point a,struct point b) 11 { 12 return a.l<b.l; 13 } 14 int main() 15 { 16 double m,d,tmpr; 17 int i,n,ok,cnt,times=0; 18 while (scanf("%d %lf",&n,&d)!=EOF) 19 { 20 times++; 21 ok=1; 22 if (n==0&&d==0) break; 23 for (i=1; i<=n; i++) 24 { 25 scanf("%lf %lf",&p[i].x,&p[i].y); 26 if (p[i].y>d) ok=0; 27 p[i].l=p[i].x-sqrt(d*d-p[i].y*p[i].y); 28 p[i].r=p[i].x+sqrt(d*d-p[i].y*p[i].y); 29 } 30 if (ok) 31 { 32 sort(p+1,p+n+1,cmp); 33 tmpr=p[1].r; 34 cnt=1; 35 for (i=2; i<=n; i++) 36 { 37 if (p[i].l>tmpr) tmpr=p[i].r,cnt++; 38 if (p[i].r<tmpr) tmpr=p[i].r; 39 } 40 printf("Case %d: %d ",times,cnt); 41 } 42 else printf("Case %d: -1 ",times); 43 } 44 return 0; 45 }