Greedy on the interval, storing the range that includes the point
#include <iostream> #include <algorithm> #include <cmath>using namespace std;struct point{double x1, X2;} P[1005];<span style= "color: #008000;" >//</span><span style= "color: #008000;" > Each island is a circle with a radius of D, </span>bool CMP (point A,point B) {return a.x1<b.x1;} with the x-axis segment cut. int main () {int n,r,i,t,end_flag,count;double temp,x,y;t=1;while (cin>>n>>r) {if (n==0&&r==0) Break;end_flag=0;count=1;for (i=1;i<=n;i++) {cin>>x>>y;if (y>r) end_flag=1;p[i].x1=x-sqrt (r*r-y* Y);p [I].x2=x+sqrt (r*r-y*y);} Stores the range that includes the point cout<< "case" <<t++<< ":"; if (End_flag) {cout<<-1<<endl;continue;} Sort (p+1,p+n+1,cmp); Temp=p[1].x2;for (i=2;i<=n;i++) {if (p[i].x2<temp) temp=p[i].x2;//If the next interval is inside the current interval. else if (p[i].x1>temp)//If the next interval is not within the current range. {count++;temp=p[i].x2;} The radar is still limited to the right end of the original interval without considering the intersection of the two intervals. }cout<<count<<endl;} return 0;}
POJ 1328 Radar installation (greedy)