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 ...
随机推荐
- springboot第一个项目【创建】
1.new project,不勾选create from archetype,直接选择 2.next下一步 在Maven依赖管理中,唯一标识一个依赖项是由该依赖项的三个属性构成的,分别是groupId ...
- confluence搭建破解及汉化教程
注:本文参考了 < confluence搭建破解及汉化教程 > 本文是在yum环境搭建好,且可用联网的前提下进行的实际操作并作记录的. 关于yum本地环境搭建可以参考此文:<Cen ...
- pytorch中的 requires_grad和volatile
https://blog.csdn.net/u012436149/article/details/66971822 简单总结其用途 (1)requires_grad=Fasle时不需要更新梯度, 适用 ...
- npx简介(转载)
npm v5.2.0引入的一条命令(npx),引入这个命令的目的是为了提升开发者使用包内提供的命令行工具的体验. 举例:使用create-react-app创建一个react项目. 老方法: npm ...
- nginx官方模块之http_random_index_module
作用 目录中选择一个随机主页 语法
- dubbo源码之服务消费
消费端启动初始化过程: 消费端的代码解析也是从配置文件解析开始的,服务发布对应的<dubbo:service,解析xml的时候解析了一个ServiceBean,并且调用ServiceConfig ...
- WEB漏洞 XSS(一)
1.xss的形成原理 xss 中文名是“跨站脚本攻击”,英文名“Cross Site Scripting”.xss也是一种注入攻击,当web应用对用户输入过滤不严格,攻击者写入恶意的脚本代码(HTML ...
- poj2728 生成树01分数规划 (二分答案)
给定整数序列a,b,求出下式的最大值 sum{ai*xi}/sum{bi*xi},xi=0|1 通俗来说,就是选出一些整数对(ai,bi),使得选出的a之和与选出的b之和商最大化 二分答案L,即选出的 ...
- Ubuntu点击dash home就崩溃
很崩溃的一个问题,搞了好久.并没有很清楚的知道具体哪个细节导致的问题,只是大概知道了原因,以及搞出了一个解决方案. 问题描述 台式机,没有独立显卡,也就是只有一个intel CPU在一起的小破显卡(我 ...
- 删除Apache服务的命令
转到\Apache24\bin目录下,使用cmd命令sc delete apache2.2