poj 1328 Radar Installation (简单的贪心)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 42925 | Accepted: 9485 |
Description
We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates.
Figure A Sample Input of Radar Installations
Input
The input is terminated by a line containing pair of zeros
Output
Sample Input
3 2
1 2
-3 1
2 1 1 2
0 2 0 0
Sample Output
Case 1: 2
Case 2: 1
Source
题目链接:http://poj.org/problem?id=1328
题意:有一条直的海岸线,上面有雷达。以海岸线为x轴,x轴上面为海,下面为岸。海里面有很多岛屿。已知雷达的观测半径。问最少建多少个雷达能把所有岛屿都覆盖到雷达
的侦测范围内。如果不能覆盖全部的岛屿,按样例输出Case数和-1,反之输出Case数和最少需要建立的雷达数。
分析:首先算出经过每个雷达且平行于x轴的弦:假设雷达覆盖半径为d。则弦的左端点为x-sqrt (d*d-y*y),右端点为x+sqrt(d*d-y*y)。然后按左端点从小到大排序。由于不能受右
端点的影响,将弦的左右端点横坐标用结构体存起来。然后,将第一个雷达放在排序后的弦投影在x轴的右端点temp。从过第2个岛屿的弦的投影开始扫描,如果右端点
<temp即右端点在雷达左边。把雷达移动到此右端点,此时就能使雷达既覆盖到这个岛屿,又覆盖到前面的岛屿。如果左端点在雷达右边,则不能覆盖,需要再建立一个
雷达。然后把新雷达建在此时弦右端点投影到x轴的坐标。忽略前面的,对后面的岛屿进行同样的操作。(贪心思想)
注意:半径和坐标都有可能是小数,所以用double类型比较好,不然用int又要注意计算时*1.000变为浮点数。
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std; struct point
{
double left;
double right;
}p[5012];
bool flag;
int cnt; bool cmp(point &x,point &y)
{
return x.left<y.left;
} int main()
{
int cases=0,i,n;
double d;
while(scanf("%d%lf",&n,&d)&&n&&d)
{
flag=true;
double a,b;
for(i=0;i<n;i++)
{
scanf("%lf%lf",&a,&b);
if(fabs(b)>d)
flag=false;
p[i].left=a-sqrt(d*d-b*b);
p[i].right=a+sqrt(d*d-b*b);
}
printf("Case %d: ",++cases);
if(!flag)
puts("-1");
else
{
sort(p,p+n,cmp);
double temp=p[0].right;
cnt=1;
for(i=1;i<n;i++)
{
if(p[i].right<temp)
temp=p[i].right;
else if(p[i].left>temp)
{
cnt++;
temp=p[i].right;
}
}
printf("%d\n",cnt);
}
}
return 0;
} //AC
11920732 |
Accepted |
188K |
32MS |
981B |
2013-08-05 00:20:15 |
poj 1328 Radar Installation (简单的贪心)的更多相关文章
- 贪心 POJ 1328 Radar Installation
题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...
- POJ 1328 Radar Installation 贪心 A
POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...
- poj 1328 Radar Installation(nyoj 287 Radar):贪心
点击打开链接 Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43490 Accep ...
- poj 1328 Radar Installation(贪心)
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- POJ 1328 Radar Installation 【贪心 区间选点】
解题思路:给出n个岛屿,n个岛屿的坐标分别为(a1,b1),(a2,b2)-----(an,bn),雷达的覆盖半径为r 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的 ...
- poj 1328 Radar Installation【贪心区间选点】
Radar Installation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) ...
- POJ 1328 Radar Installation【贪心】
POJ 1328 题意: 将一条海岸线看成X轴,X轴上面是大海,海上有若干岛屿,给出雷达的覆盖半径和岛屿的位置,要求在海岸线上建雷达,在雷达能够覆盖全部岛屿情况下,求雷达的最少使用量. 分析: 贪心法 ...
- poj 1328 Radar Installation(贪心+快排)
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- poj 1328 Radar Installation 排序贪心
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 56702 Accepted: 12 ...
随机推荐
- REST API 基于ACCESS TOKEN
REST API 基于ACCESS TOKEN 的权限解决方案 REST 设计原则是statelessness的,而且但客户端是APP时,从APP发起的请求,不是基于bowers,无法带相同的se ...
- hdu 1251 统计拼图
二手tire木: Basic应用程序 谈到很具体的 点击打开链接 #include<cstdio> #include<cstring> #include<iostream ...
- 在将 varchar 值 '2,7' 转换成数据类型 int 时失败
消息 245,级别 16,状态 1,第 1 行在将 varchar 值 '2,7' 转换成数据类型 int 时失败. 原sql select UserName from s_User where ...
- 我已提取并尝试使用启动脚本(./start navicat)来启动 Navicat Linux 版本号,但没有反应
具体的安装教程,參考这个navicat_for_mysql_10.0.11在linux下的安装,介绍的非常具体 參考这个 :我可否在 64-bit Linux 执行 Navicat? 推荐navica ...
- WPF技术触屏上的应用系列(一): 3D 图片(照片)墙、柱面墙(凹面墙或者叫远景墙、凸面墙或者叫近景墙)实现
原文:WPF技术触屏上的应用系列(一): 3D 图片(照片)墙.柱面墙(凹面墙或者叫远景墙.凸面墙或者叫近景墙)实现 去年某客户单位要做个大屏触屏应用,要对档案资源进行展示之用.客户端是Window7 ...
- Oracle的海量存储技术
下午去參加一个Oracle有关海量数据存储技术的培训讲座了. 地址在广州市林和西路101号天河区计经大楼西側三楼. 培训发起机构为:广州中睿信息技术有限公司. 以下就简要总结一下所听到的一些东西,也算 ...
- 写一个 docker 打击一系列手册
感谢您的关注,分享也再次给自己一个学习的.机会组织和总结.对未来一段时间内准备一个关于 docker 一系列的实际应用,其中的一些内容此前曾宣布.准备再次修改和整理. 以下是主要的文件夹中的一个: 创 ...
- lua.c:80:31: fatal error: readline/readline.h: No such file or directory
make linuxcd src && make linuxmake[1]: Entering directory `/root/lua/lua-5.3.2/src'make all ...
- STL内存分配
STL内存创建 Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu 转载请标明来源 1. Stl内存创建基类模板__malloc_alloc_tem ...
- [Unity3D] 有关公告板实现的误区
最直接实现一个公告板,我认为我们应该这样做: usingUnityEngine; publicclassBillboard :MonoBehaviour { voidUpdate() { transf ...