poj 1328

题意:给你一个二维坐标,y轴上是海,y轴下是陆地,然后给你一些在海上的小岛,然后有一种半径为d圆形雷达,圆心只在x轴上,问最少需要多少个雷达能覆盖所有小岛。

我开始的思路:我先以x轴从左到右排序,然后以最左边的小岛建立雷达(假设坐标为x,y),第一个雷达的圆心是(x+sqrt(d*d+y*y));然后判断下一个点是否在这个圆内,不在的话就重新以这个小岛更新一个圆心然后计数器+1;但是WA了2次,思路的话也不知道错在哪里;

最后看了别的大牛的思路;说是区间覆盖问题;看代码理解吧

 

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 int n,d;
 8 struct coord{
 9     double left,right;
10 }p[2015],temp;
11 bool operator < (coord a,coord b){
12     return a.left < b.left;
13 }
14 int main()
15 {
16     int n,d,t=0;
17     while(cin >> n >> d&&(n||d)){
18         int flag=1;
19         for(int i=0;i<n;++i){
20             double a,b;
21             cin >> a >> b;
22             if(fabs(b)>d)    flag=0;
23             else{
24                 double c=sqrt(d*d-b*b);
25                 p[i].left=a*1.0-c;p[i].right=a*1.0+c; 
26             }
27         }
28         cout << "Case " << ++t << ": ";
29         if(!flag)    cout << -1 << endl;
30         else{
31             int count=1;
32             sort(p,p+n);temp=p[0];
33             for(int i=1;i<n;++i){
34                 if(p[i].left>temp.right){        //不重叠的话就要开始找新圆了 
35                     ++count;temp=p[i];
36                 }
37                 else if(p[i].right<temp.right)        //更新最小的左边的点; 
38                     temp=p[i];
39             }
40             cout << count << endl; 
41         }
42     }
43 }

 

好不容易看懂了题意,可惜做不出来;

好弱好弱阿、

 

posted @ 2016-01-13 00:54  我不萌、我要高冷  阅读(176)  评论(0编辑  收藏  举报