poj1328 Radar Installation 区间贪心

题目大意:

  在X轴选择尽量少的点作为圆心,作半径为d的圆。使得这些圆能覆盖所有的点。

思路:

  把每个点都转化到X轴上。也就是可以覆盖这个点的圆心的位置的范围[a,b]。然后按照每个点对应的a从小到大排序。第一点需要特殊处理,我们赋值r=b。也就是使得第一个圆的圆心的横坐标尽量大。然后遍历剩下的点。对于i点,如果该点的ai大于r, 就需要增加一个圆,圆心为bi ;否则的话,把r更新为r与bi中小的值。

代码:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7
 8 const int N = 1010;
 9 struct node
10 {
11     double x,y;
12     double a,b;
13 }p[N];
14 bool cmp(node n1,node n2)
15 {
16     return n1.a<n2.a;
17 }
18 int main()
19 {
20     //freopen("test.txt","r",stdin);
21     int n,d,ca=1;
22     while(scanf("%d%d",&n,&d)!=EOF&&n)
23     {
24         int i;
25         bool flag=0;
26         for(i=0;i<n;i++)
27         {
28             scanf("%lf%lf",&p[i].x,&p[i].y);
29             if(p[i].y>d) flag=1;
30         }
31         printf("Case %d: ",ca++);
32         if(flag) printf("-1\n");
33         else
34         {
35             for(i=0;i<n;i++)
36             {
37                 double x=p[i].x,y=p[i].y;
38                 p[i].a=-sqrt(d*d-y*y)+x;
39                 p[i].b=sqrt(d*d-y*y)+x;
40             }
41             sort(p,p+n,cmp);
42             double r=p[0].b;
43             int ans=1;
44             for(i=1;i<n;i++)
45             {
46                 if(p[i].a>r){
47                     r=p[i].b;
48                     ans++;
49                 }
50                 if(p[i].b<r) r=p[i].b;
51             }
52             printf("%d\n",ans);
53         }
54     }
55     return 0;
56 }

时间: 2024-10-23 14:35:08

poj1328 Radar Installation 区间贪心的相关文章

POJ1328 Radar Installation 【贪心&amp;#183;区间选点】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54593   Accepted: 12292 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

poj1328 Radar Installation(贪心)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1328 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in th

POJ1328 Radar Installation 【贪心&#183;区间选点】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54593   Accepted: 12292 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

zoj1360/poj1328 Radar Installation(贪心)

对每个岛屿,能覆盖它的雷达位于线段[x-sqrt(d*d-y*y),x+sqrt(d*d+y*y)],那么把每个岛屿对应的线段求出来后,其实就转化成了经典的贪心法案例:区间选点问题.数轴上有n个闭区间[ai,bi],取尽量少的点,使得每个区间内都至少有一个点.选法是:把区间按右端点从小到大排序(右端点相同时按左端点从大到小),然后每个没选的区间选最右边的一个点即可. #include<iostream> #include<cstdio> #include<cstdlib>

POJ 1328 Radar Installation 【贪心 区间选点】

解题思路:给出n个岛屿,n个岛屿的坐标分别为(a1,b1),(a2,b2)-----(an,bn),雷达的覆盖半径为r 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的位置在x轴上,所以我们设雷达的坐标为(x,0),对于任意一个岛屿p(a,b),因为岛屿要满足在雷达的覆盖范围内,所以 (x-a)^2+b^2=r^2,解得 xmin=a-sqrt(r*r-b*b);//即为区间的左端点 xmax=a+sqrt(r*r-b*b);//即为区间的右端点 接下来区间选点即

poj 1328 Radar Installation 【贪心】【区间选点问题】

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54798   Accepted: 12352 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

poj 1328 Radar Installation【贪心区间选点】

Radar Installation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Submission(s) : 22   Accepted Submission(s) : 9 Problem Description Assume the coasting is an infinite straight line. Land is in one side of coa

poj 1328 Radar Installation (贪心)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52823   Accepted: 11883 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point loca

POJ1328——Radar Installation

Radar Installation Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only