Radar Installation POJ 1328(贪心)

原题

题目链接

题目分析

把每个点处理成一个区间[li,ri],其中li表示x-sqrt(d*d-y*y),ri表示x+sqrt(d*d-y*y),前提是d>=y,否则雷达无法检测到该点直接输出-1.处理完之后,把所有区间按左端点值排个序,排完后每取一个区间[s,t],就把其他区间中si<t的过滤掉,因为如果它们区间重叠,则用一个雷达就可以把这些重叠区间全部检测到,就这样遍历一遍所有区间,最后统计的ans就是所需最小雷达数.

代码

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <utility>
 4 #include <cstdio>
 5 #include <cmath>
 6 #include <cstring>
 7 #include <string>
 8 #include <vector>
 9 #include <stack>
10 #include <queue>
11 #include <map>
12 #include <set> 
13 
14 using namespace std;
15 typedef long long LL;
16 const int INF_INT=0x3f3f3f3f;
17 const LL INF_LL=0x3f3f3f3f3f3f3f3f; 
18 typedef pair<double,double> P;
19 
20 P point[1000];
21 
22 bool cmp(P a,P b)
23 {
24     if(a.first==b.first) return a.second<b.second;
25     return a.first<b.first;
26 }
27 
28 int main()
29 {
30 //    freopen("black.in","r",stdin);
31 //    freopen("black.out","w",stdout);
32     int n;
33     double d;
34     int cnt=1;
35     while(~scanf("%d %lf",&n,&d)&&(n||d))
36     {
37         bool illegal=false;
38         for(int i=0;i<n;i++)
39         {
40             scanf("%lf %lf",&point[i].first,&point[i].second);
41             if(point[i].second<=d)
42             {
43                 double dx=sqrt(d*d-point[i].second*point[i].second);
44                 point[i].second=point[i].first+dx;
45                 point[i].first-=dx;
46 //                printf("%d %.3f %.3f\n",i+1,point[i].first,point[i].second);
47             }
48             else illegal=true;
49         }
50         if(illegal) printf("Case %d: -1\n",cnt++);
51         else
52         {
53             sort(point,point+n,cmp);
54     //        for(int i=0;i<n;i++) printf("%d %.3f %.3f\n",i+1,point[i].first,point[i].second);
55             int ans=0;
56             int i=0;
57             while(i<n)
58             {
59                 ans++;
60                 double righ=point[i].second;
61             //    printf("right %.3f\n",righ);
62                 while(point[i].first<=righ&&i<n)
63                 {
64                     if(point[i].second<righ) righ=point[i].second;//printf("right %.3f\n",righ);
65                     i++;
66                 }
67             }
68             printf("Case %d: %d\n",cnt++,ans);
69         }
70     }
71     return 0;
72 } 

猜你喜欢

转载自www.cnblogs.com/VBEL/p/11398901.html