【贪心】POJ1328-Radar Installation

【思路】

以每一座岛屿为圆心,雷达范围为半径作圆,记录下与x轴的左右交点。如果与x轴没交点,则直接退出输出“-1”。以左交点为关键字进行排序,从左到右进行贪心。容易知道,离每一个雷达最远的那一座岛与雷达相距恰巧为半径的时候,可以得到最优解。假设上一个雷达与第before座岛相距为半径大小,对于当前的岛屿i:

如果before岛的右交点在i岛左交点的左侧,此时必然需要一个新的雷达,将当前before暂定为i,雷达数加一;

如果before岛的右交点在i岛右交点的右侧,为了让雷达举例尽可能地广,将雷达移至当前岛屿与x轴的交点上,将当前before暂定为i,雷达数不变。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 using namespace std;
 6 double sqrt(double n);
 7 struct rec
 8 {
 9     double left,right;
10     bool operator < (const rec& x) const
11     {
12         return left<x.left;
13     }
14 };
15
16 const int MAXN=1000+5;
17 int n,d;
18 rec inter[MAXN];
19
20 int calculate()
21 {
22     sort(inter,inter+n);
23     int ans=1;
24     int before=0;
25     for (int i=1;i<n;i++)
26     {
27         if (inter[before].right<inter[i].left)
28         {
29             ans++;
30             before=i;
31         }
32         else if (inter[before].right>=inter[i].right)
33         {
34             before=i;
35         }
36     }
37     return ans;
38 }
39
40 int main()
41 {
42     int t=0;
43     while (scanf("%d%d",&n,&d))
44     {
45         if (n==d && d==0) break;
46         t++;
47         int f=1;
48         for (int i=0;i<n;i++)
49         {
50             double x,y;
51             cin>>x>>y;
52             if (d<y)
53             {
54                 f=0;
55             }
56             else
57             {
58                 inter[i].left=x*1.0-sqrt(d*d-y*y);
59                 inter[i].right=x*1.0+sqrt(d*d-y*y);
60             }
61         }
62         cout<<"Case "<<t<<": ";
63         if (f) cout<<calculate()<<endl;else cout<<-1<<endl;
64
65     }
66     return 0;
67 }
时间: 2024-08-06 11:30:40

【贪心】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

[POJ1328] 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 cover d distance, s

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

贪心——D - Radar Installation

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 cover d distance, so an island

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>

poj1328 Radar Installation 区间贪心

题目大意: 在X轴选择尽量少的点作为圆心,作半径为d的圆.使得这些圆能覆盖所有的点. 思路: 把每个点都转化到X轴上.也就是可以覆盖这个点的圆心的位置的范围[a,b].然后按照每个点对应的a从小到大排序.第一点需要特殊处理,我们赋值r=b0 .也就是使得第一个圆的圆心的横坐标尽量大.然后遍历剩下的点.对于i点,如果该点的ai大于r, 就需要增加一个圆,圆心为bi :否则的话,把r更新为r与bi中小的值. 代码: 1 #include<iostream> 2 #include<cstdio

ZOJ-1360 || POJ-1328——Radar Installation

ZOJ地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=360 POJ地址:http://poj.org/problem?id=1328 解题思路:贪心 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <math.h> 5  6 struct P{ 7     double x, y

Radar Installation(poj1328)(贪心)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54756   Accepted: 12337 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