For each island, the radar that can cover it is located in the line segment [X-sqrt (D*d-y*y), X+sqrt (D*d+y*y)], then the corresponding line of each island to find out, in fact, transformed into a classic greedy bill example: Interval location point problem. There are n closed intervals [Ai,bi] on the axis, taking as few points as possible, so that there is at least one point in each interval. The selection method is: The interval by the right end point from small to large (the right end of the same time by the left endpoint from the largest to the smallest), and then each not selected interval select the rightmost point.
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<cmath>#include<map>#include<Set>#include<list>#include<deque>#include<vector>#include<algorithm>#include<stack>#include<queue>#include<cctype>#include<sstream>using namespacestd;#definePII pair<int,int>#defineLL Long Long intConst Doubleeps=1e-Ten;Const intinf=1000000000;Const intmaxn= ++Ten;intn,cas=1;Doubled,x,y;structline{DoubleL,r; BOOLused; Line (DoubleLl=0,DoubleRr=0,BOOLuu=false): L (LL), R (RR), Used (UU) {} line (Constline&C) {L=C.L; R=C.R; Used=c.used; } BOOL operator< (Constline& B)Const { if(r!=B.R)returnr<B.R; Else returnL>B.L; }} A[MAXN];intMain () {//freopen ("In2.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout); while(SCANF ("%D%LF", &n,&d) = =2) { if(n==0&&d==0) Break; printf ("Case %d:", cas++); intcn1=0; for(intI=0; i<n; i++) {scanf ("%LF%LF",&x,&y); if(y>D) {ans=-1; Break; } A[I].L=X-SQRT (d*d-y*y); A[I].R=X+SQRT (d*d-y*y); //cout<<a[i].l<< ' <<a[i].r<<endl;A[i].used=false; } if(ans==-1) {printf ("-1\n"); } Else{sort (a,a+N); for(intI=0; i<n; i++) { //cout<<a[i].l<< ' <<a[i].r<<endl; if(a[i].used==false) {a[i].used=true; Ans++; for(intj=i+1; j<n; J + +) { if(a[j].l<=A[I].R) {a[j].used=true; } Else Break; }}} printf ("%d\n", ans); } } //fclose (stdin); //fclose (stdout); return 0;}
zoj1360/poj1328 Radar Installation (greedy)