poj 1328 Radar Installation(贪心)

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

 

题意:建立一个平面坐标,x轴上方是海洋,x轴下方是陆地。在海上有n个小岛,每个小岛看做一个点。然后在x轴上有雷达,雷达能覆盖的范围为d,问至少需要多少个雷达能监测到多有的小岛。

思路:从左到右把每个小岛的放置雷达的区间求出,按结束点排序,从左至右看,当发现下一个区间的起始点大于前面所有区间的最小结束点的时候,答案加一。

 1 #include <stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #include<math.h>
 5 
 6 using namespace std;
 7 struct node
 8 {
 9    double a,b;
10 }area[1005];
11 
12 int cmp(const void *a,const void *b)
13 {
14     return (*(node *)a).b>(*(node *)b).b?1:-1;
15 }
16 
17 int main()
18 {
19     int n,i,sum,f,h=1;
20     double x,y,d,e;
21     while(~scanf("%d%lf",&n,&d)&&(n!=0||d!=0))
22     {
23          f=0;   sum=1;
24         for(i=0; i<n; i++)
25         {
26             scanf("%lf%lf",&x,&y);
27             area[i].a=x-sqrt(d*d-y*y);
28             area[i].b=x+sqrt(d*d-y*y);
29             if(y<0)
30             y=-y;
31             if(y>d)
32             f=1;
33         }
34 
35         if(f)
36         printf("Case %d: -1
",h);
37 
38         else
39         {
40             qsort(area,n,sizeof(area[0]),cmp);
41             e=area[0].b;
42             for(i=1; i<n; i++)
43             {
44                 if(area[i].a>e)
45                 {
46                     sum++;
47                     e=area[i].b;
48                 }
49             }
50             printf("Case %d: %d
",h,sum);
51         }
52         h++;
53     }
54     return 0;
55 }
原文地址:https://www.cnblogs.com/bfshm/p/3148937.html