The main topic: There are N islands in the sea, now want to install radar on the coastline to detect these islands (the coastline is equivalent to the x axis, the island is above the x-axis), the radar detection range is a radius d circle, known N Island coordinates, ask you at least how much radar to detect all the islands If the entire island cannot be detected, the output "-1".
Analysis:
Obviously, if the input of the island's y-coordinates is greater than the radar detection range, then we can directly determine the impossible to detect all the islands (because the radar is always on the coastline).
As for all the islands that can be detected, we know that each given the coordinates of each island, we can all find so two radars on the coastline, so that the island happens to be on the left and right edges of the two radars ' range of detection, and we record these two marginal values as the interval values that can be detected on the coastline. , so that after the coordinates of the N islands are determined, we have identified n interval values to detect the islands, and then we have to filter out the effective interval values, that is, the minimum number of radars.
Implementation code:
#include <cstdio> #include <algorithm> #include <iostream> #include <cmath> using namespace std; typedef struct Node {double left,right;}
Interval
Interval ans[1010];
int cmp (const interval &a,const interval &b) {return a.left<b.left;} int main () {int n,x,y;
Double D;
BOOL Flag;
int t=1;
while (scanf ("%d%lf", &n,&d)) {if (n==0&&d==0) break;
Flag=true;
for (int i=0;i<n;i++) {scanf ("%d%d", &x,&y);
if (y>d) Flag=false;
ANS[I].LEFT=X*1.0-SQRT (D*d-y*y);
ANS[I].RIGHT=X*1.0+SQRT (D*d-y*y);
printf ("Case%d:", t++);
if (!flag) puts ("-1");
else {sort (ans,ans+n,cmp);
int cnt=1;
Interval Tmp=ans[0];
for (int i=1;i<n;i++) if (ans[i].left>tmp.right) {cnt++;
Tmp=ans[i]; else if (ans[i].right<tmp.right) tmp=ans[i];
printf ("%d\n", CNT);
} return 0;
}