UVA 2519 Radar Installtion

思路:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<cstring>
 6 #include<cstdlib>
 7 using namespace std;
 8 struct tt{
 9     double l,r;//左右指针 
10 }p[1010];//线段序列
11 /*
12 第i个岛屿被雷达覆盖的水平线段为[p[i].l,p[i].r] 
13 */ 
14 
15 int n,d;//岛屿数,雷达覆盖距离 
16 
17 void input()
18 {
19     double x,y,h;
20     for(int i=0;i<n;i++)
21     {
22         scanf("%lf%lf",&x,&y);
23         if(y>d)//岛屿离岸边距离大于雷达距离 
24         {
25             d=-1;
26             return;
27         }
28         h=sqrt(d*d-y*y);//计算岛屿线段半长 
29         p[i].l=x-h;//设置左右指针 
30         p[i].r=x+h;
31     }
32 }
33 //以右端点为第一,左端点为第二关键字,递增排序 
34 bool cmp(const tt &a,const tt &b)
35 {
36     if(a.r<b.r)    
37     return true;
38     if(a.r==b.r&&(a.l<b.l))
39     return true;
40     return false;
41 } 
42 
43 void work()
44 {
45     if(d==-1)
46     {
47         printf("-1\n");
48         return;
49     }
50     sort(p,p+n,cmp);
51     int ans=0;
52     double last=-100000000;//安装雷达的位置 
53     for(int i=0;i<n;i++)
54     {
55         if(p[i].l<=last)//此位置已经有雷达
56             continue;
57         ans++;
58         last=p[i].r;//没有雷达就在右边放一个 
59     }
60     printf("%d\n",ans);
61 } 
62 
63 int main()
64 {
65     int cnt=0;
66     while(scanf("%d%d",&n,&d),n+d)
67     {
68         printf("Case %d: ",++cnt);
69         input();
70         work();
71     }
72     return 0;
73 }

 

posted @ 2019-02-27 21:29  付玬熙  阅读(274)  评论(0编辑  收藏  举报