Question:
There are n coordinates of the island above the X axis, and the radar range is also given. Now we need to put as few radar as possible on the X axis so that the island will be covered with the least number of questions.
Question:
Greedy. Determine the ing range of each island on the X axis (a radar in this range can scan to the Island), then sort by the right border, and then look like a monotonous Queue (of course there is more water ), try to move the radar to the right.
Details:
Note 1:
Radar doesn't have to be on the whole point. Don't be cheated.
NOTE 2:
If the data is incorrect, output-1
1. Some points cannot be scanned
2. Some points are below the X axis
III. D <0
Code:
#include <cmath>#include <cstdio>#include <cstring>#include <algorithm>#define N 1050#define inf 0x3f3f3f3fusing namespace std;int n,d;struct KSD{int x,y;double l,r;bool init(){int temp=d*d-y*y;if(temp<0)return 1;l=x-sqrt(temp);r=x+sqrt(temp);return 0;}bool operator < (const KSD &a)const{return r<a.r;}}s[N];int main(){//freopen("test.in","r",stdin);int i,t=0,flag;while(scanf("%d%d",&n,&d),n||d){printf("Case %d: ",++t);flag=0;if(d<0)flag=1;for(i=1;i<=n;i++){scanf("%d%d",&s[i].x,&s[i].y);if(s[i].y<0||s[i].init())flag=1;}if(flag){puts("-1");continue;}sort(s+1,s+n+1);double now=-999999999.9;int ans=0;for(i=1;i<=n;i++){if(s[i].l<=now)continue;now=s[i].r;ans++;}printf("%d\n",ans);}return 0;}
[Poj1328] radar installation greedy