POJ-1328 Radar Installation-贪心算法
Description
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.
![](http://poj.org/images/1328_1.jpg)
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
#include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; struct Island { double left; double right; }island[1100]; bool cmp(Island a,Island b) { return a.left<b.left; } int main() { int n,d,i,j,t,temp=0; double x,y,fuck; while(~scanf("%d%d",&n,&d),n+d) { int m=0; temp++; for(i=0;i<n;i++) { scanf("%lf %lf",&x,&y); if(y>d||d<0) { m=1; } island[i].left=x-(sqrt(d*d-y*y)); island[i].right=x+(sqrt(d*d-y*y)); } if(m) { printf("Case %d: -1\n",temp); continue; } sort(island,island+n,cmp); t=1; fuck=island[0].right; for(i=1;i<n;i++) { if(island[i].right<=fuck) { fuck=island[i].right; } else if(island[i].left>fuck) { t++; fuck=island[i].right; } } printf("Case %d: %d\n",temp,t); } return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。