POJ - 1328 Radar Installation
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 is terminated by a line containing pair of zeros
Output
Sample Input
3 2 1 2 -3 1 2 1 1 2 0 2 0 0
Sample Output
Case 1: 2 Case 2: 1
以各个小岛为圆心,d为半径做圆,与x轴的两个交点记录,排序,贪心从左到右找出最少需要的雷达。
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 7 using namespace std; 8 9 struct node 10 { 11 double l,r; 12 }c[1005]; 13 14 bool cmp(node a,node b) 15 { 16 return a.l<b.l; 17 } 18 19 int main() 20 { 21 int n,d,x,y,s=1; 22 while(scanf("%d%d",&n,&d),n||d) 23 { 24 int flag=0; 25 memset(c,0,sizeof(c)); 26 for(int i=0;i<n;i++) 27 { 28 scanf("%d%d",&x,&y); 29 if(flag) continue; 30 if(y>d) 31 flag=1; 32 else 33 { 34 c[i].l=(double)x-sqrt((double)d*d-y*y); 35 c[i].r=(double)x+sqrt((double)d*d-y*y); 36 } 37 } 38 if(flag) 39 { 40 printf("Case %d: -1\n",s++); 41 continue; 42 } 43 sort(c,c+n,cmp); 44 int ans=1; 45 double h=c[0].r; 46 for(int i=1;i<n;i++) 47 { 48 if(c[i].r<h) 49 h=c[i].r; 50 else if(c[i].l>h) 51 { 52 h=c[i].r; 53 ans++; 54 } 55 } 56 printf("Case %d: %d\n",s++,ans); 57 } 58 59 60 return 0; 61 }