Radar Installation POJ - 1328(贪心)
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 题意:N座岛屿,坐标用xi,yi表示,岛屿都位于x轴上。在x轴上建造雷达,每个雷达的的探测范围是以【x,0】为圆心,半径为R的圆(下半圆一看就没用),问最少几个雷达可以探测全部岛屿? 思路:我们把每个岛屿为圆心,做出x轴上雷达的可行建造区域,那么问题就变成用最小的雷达覆盖最多的区域(每个区域一个雷达)。
我们先将区域按左边界递增排序,每取到一个区间,就记录在这个区间右边界pos,如果下个区间的左边界>pos,说明这个雷达无法探测,需要新建雷达,
否则就pos = min(pos,r),就是让原有的雷达去探测它,但是雷达需要移至两个区间的最小右边界处(交集嘛)。 ①因为要用比较左边界和上一次区间的右边界,所以一定是按照左边界排序的。(这样处理后面的区域和之前区域没啥关系,只和pos有关)
置于为什么pos取右边界,因为右边界代表了雷达最远可放置位置(极值),其包含了区间内可行取值了。(决策包容性?) ②当然如果你反过来,右边界排序,你就需要比较右边界和上一次的左边界也行。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std; int n,r;
struct Node
{
double l,r;
} L[]; double calc(int y,int r)
{
return sqrt(r*r-y*y);
} int dcmp(double a,double b)
{
if(fabs(a-b) < 1e-)
return ;
else
return a-b > ?:-;
}
bool cmp(Node a,Node b)
{
return dcmp(a.l,b.l)<;
}
int main()
{
int cas = ;
while(~scanf("%d%d",&n,&r) && (n || r))
{
int ans=;
for(int i=; i<=n; i++)
{
double x,y;
scanf("%lf%lf",&x,&y);
if(dcmp(y,r) > )
{
ans = -;
continue;
}
double add = calc(y,r);
L[i].l = x-add;
L[i].r = x+add;
}
if(ans == -){printf("Case %d: -1\n",++cas);continue;}
sort(L+,L++n,cmp);
double pos = -0x3f3f3f3f3f3f3f;
for(int i=; i<=n; i++)
{
if(dcmp(L[i].l,pos)>)
{
ans++;
pos = L[i].r;
}
else
{
pos = min(L[i].r,pos);
}
}
printf("Case %d: %d\n",++cas,ans);
}
}
Radar Installation POJ - 1328(贪心)的更多相关文章
- Radar Installation POJ - 1328
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...
- Radar Installation POJ - 1328 (贪心)
题目大意(vj上的翻译版本) 假定海岸线是无限长的直线.陆地位于海岸线的一侧,海洋位于另一侧.每个小岛是位于海洋中的一个点.对于任何一个雷达的安装 (均位于海岸线上),只能覆盖 d 距离,因此海洋中的 ...
- Greedy:Radar Installation(POJ 1328)
装雷达 题目大意,就是令在海岸线的(直线)一边是海(y>0),另一边是陆地(y<=0),在海岸线上装雷达,雷达可以覆盖的范围为d,海上有岛,(x,y),问你应该怎么装雷达,才能做到技能雷达 ...
- poj 1328 Radar Installation (简单的贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42925 Accepted: 94 ...
- poj 1328贪心
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- poj 1328 贪心
/* 贪心.... 处理处每个点按照最大距离在x轴上的映射 然后我们就有了一些线段 目的是选取尽量少的点 使得每个线段内都有点出现 我们按照左端点排序 然后逐一处理 假设第一个雷达安在第一个线段的右端 ...
- poj1328 Radar Installation —— 贪心
题目链接:http://poj.org/problem?id=1328 题解:区间选点类的题目,求用最少的点以使得每个范围都有点存在.以每个点为圆心,r0为半径,作圆.在x轴上的弦即为雷达可放置的范围 ...
- 贪心 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 ...
随机推荐
- ORACL内部异常:
ORACL内部异常: ORA-00001: 违反唯一约束条件 (.) ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会话数 ORA-00019: 超出最大会话许可数 OR ...
- OCM 学习练习题目
1:数据安装操作练习:考试题目 1: Creating a database & Server Configuration --[101]-- #创建数据库 1. Create the dat ...
- Confluence 6 重要缓存和监控
重要缓存 下面的建议是基本上的一些配置帮助.在大型数据库中,20-30% 的数据库表大型可能是不需要如此膨胀的.在缓存配置的界面中,检查有效率和使用率的配置来进行必要的修改. 内容对象缓存(Conte ...
- 初见TensorFlow :知其所以然
2.1 TensorFlow的主要依赖包 TensorFlow依赖的两个最主要的工具包——Protocol Buffer和Bazel. 2.1.1 Protocol Buffer Protocol B ...
- 【python】查找函数定义
help(函数名) 举例:想知道gevnet.Timeout这个函数是怎么用的.help(gevent.Timeout). 之前不知道这样查,每次遇到新函数想知道有哪些参数我都要到网上疯狂查阅文档.现 ...
- mysql通配符使用
mysql通配符使用: w3cchool 在mysql查询中,经常会用到通配符,而且mysql的通配符和pgsql是有所不同的,甚至mysql中还可以使用正则表达式.本文就为大家带来mysql查询中通 ...
- Visual Studio UML类图
1.unified Modeling Language(UML)称为同一建模语言或者标准建语言, 用例图:对系统的使用方式的分类.类图:显示类和他们的相互关系. 对象图:只显示对象及他们的相互关系. ...
- linux下搭建SVN
官网下载: http://subversion.apache.org/packages.html SVN客户端:TortoiseSVN :https://tortoisesvn.net/downl ...
- OpenCV-Python入门教程6-Otsu阈值法
在说Otsu之前,先说几个概念 灰度直方图:将数字图像中的所有像素,按照灰度值的大小,统计其出现的频率.其实就是每个值(0~255)的像素点个数统计. Otsu算法假设这副图片由前景色和背景色组成,通 ...
- Nignx添加proxy_pass可能造成DNS解析超时的问题解决
resolver 219.149.194.55; location ^~ /bigdata { proxy_set_header Host $host; ...