poj 1328 Radar Installation

来源:互联网 发布:淘宝优惠卷的坑 编辑:程序博客网 时间:2024/06/11 14:53

题目:http://poj.org/problem?id=1328

题意:海岸线上建雷达站,监测范围为d,给出小岛坐标求最少雷达站数量
思路:以每个小岛为圆心做圆,与x轴交于两点,这两点所成的区间为能够监测到这个小岛的雷达站建造范围

若2个小岛圆区间有相交则雷达站建在相交区间可以同时监测那2个小岛,若不相交则结果+1

做出区间后排序,之后就是活动安排问题

#include<stdio.h>#include<stdlib.h>#include<math.h>struct Radar{double st,end;}a[1005];int cmpxy(const struct Radar *a,const struct Radar *b){if( a->end != b->end ) return a->end > b->end ? 1:-1;return a->st > b->st ? 1 : -1;}int main(){int i,n,res,count=0,j,noway;double x,y,d,len;//freopen("a.txt","r",stdin);while(scanf("%d%lf",&n,&d)!=EOF && (n||d) ){noway = 0;res = 1;count ++;for(i=0;i<n;i++){scanf("%lf%lf",&x,&y);if( y > d )noway = 1;len = sqrt( (d*d-y*y) );a[i].st = x - len;a[i].end = x + len;}qsort(a,n,sizeof(a[0]),cmpxy);if ( noway == 0 ){j = 0;for(i=1;i<n;i++)//注意i从1开始{if( a[i].st > a[j].end ){res++;j = i;}}printf("Case %d: %d\n",count,res);}elseprintf("Case %d: -1\n",count);}return 0;}


0 0