POJ1328——Radar Installation
Radar Installation
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.
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
题目大意:x轴上可以放置雷达(放置位置可为小数),以放置位置为圆心,d为半径做圆来覆盖岛屿,输出最小的雷达数以覆盖所有的岛屿。无法覆盖输出-1。
解题思路:假设某个岛屿的坐标为(x,y),则在(x-sqrt(d*d-y*y),x+sqrt(d*d-y*y))范围内的雷达可以覆盖到此岛屿。
先求出各个岛屿的可覆盖雷达范围,在根据其区间左端点进行排序,再通过贪心确定需要的最少雷达数。
ps:当一个岛屿的纵坐标大于d时,不可能覆盖到,输出-1。
ps2:进行贪心时,设置一个变量tmpr=p[1].r。
当p[i].r<tmpr时,tmpr=p[i].r 否则p[i]点会漏掉。
当p[i].l>tmpr是,tmpr=p[i].r,cnt++
Code:
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct point
{
double x,y;
double l,r;
} p[];
bool cmp(struct point a,struct point b)
{
return a.l<b.l;
}
int main()
{
double m,d,tmpr;
int i,n,ok,cnt,times=;
while (scanf("%d %lf",&n,&d)!=EOF)
{
times++;
ok=;
if (n==&&d==) break;
for (i=; i<=n; i++)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
if (p[i].y>d) ok=;
p[i].l=p[i].x-sqrt(d*d-p[i].y*p[i].y);
p[i].r=p[i].x+sqrt(d*d-p[i].y*p[i].y);
}
if (ok)
{
sort(p+,p+n+,cmp);
tmpr=p[].r;
cnt=;
for (i=; i<=n; i++)
{
if (p[i].l>tmpr) tmpr=p[i].r,cnt++;
if (p[i].r<tmpr) tmpr=p[i].r;
}
printf("Case %d: %d\n",times,cnt);
}
else printf("Case %d: -1\n",times);
}
return ;
}
POJ1328——Radar Installation的更多相关文章
- [POJ1328]Radar Installation
[POJ1328]Radar Installation 试题描述 Assume the coasting is an infinite straight line. Land is in one si ...
- POJ--1328 Radar Installation(贪心 排序)
题目:Radar Installation 对于x轴上方的每个建筑 可以计算出x轴上一段区间可以包含这个点 所以就转化成 有多少个区间可以涵盖这所有的点 排序之后贪心一下就ok 用cin 好像一直t看 ...
- POJ1328 Radar Installation 【贪心·区间选点】
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54593 Accepted: 12 ...
- POJ1328 Radar Installation 解题报告
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- poj1328 Radar Installation(贪心 策略要选好)
https://vjudge.net/problem/POJ-1328 贪心策略选错了恐怕就完了吧.. 一开始单纯地把island排序,然后想从左到右不断更新,其实这是错的...因为空中是个圆弧. 后 ...
- POJ1328 Radar Installation(贪心)
题目链接. 题意: 给定一坐标系,要求将所有 x轴 上面的所有点,用圆心在 x轴, 半径为 d 的圆盖住.求最少使用圆的数量. 分析: 贪心. 首先把所有点 x 坐标排序, 对于每一个点,求出能够满足 ...
- zoj1360/poj1328 Radar Installation(贪心)
对每个岛屿,能覆盖它的雷达位于线段[x-sqrt(d*d-y*y),x+sqrt(d*d+y*y)],那么把每个岛屿对应的线段求出来后,其实就转化成了经典的贪心法案例:区间选点问题.数轴上有n个闭区间 ...
- poj1328 Radar Installation —— 贪心
题目链接:http://poj.org/problem?id=1328 题解:区间选点类的题目,求用最少的点以使得每个范围都有点存在.以每个点为圆心,r0为半径,作圆.在x轴上的弦即为雷达可放置的范围 ...
- ZOJ-1360 || POJ-1328——Radar Installation
ZOJ地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=360 POJ地址:http://poj.org/problem?id ...
随机推荐
- Access数据库一种树形结构的实现和子节点查询
BOOL CManageDataBase::GetDepTreeAllSons( int rootItem ) { CADORecordset Rst(&m_DataBase); BOOL b ...
- 处理器在 protected mode 下的 protection
前言 实模式,保护模式,分段,分页,虚拟内存,内核态,用户态,如果你对这些术语之间的关系非常熟悉,那就不用继续看了.这篇主要记录我对用户态/内核态的一些理解,如有不对还请指教. 下述说明均为 x86- ...
- 医学影像工作站程序ProDicom的说明
转载 http://blog.csdn.net/prodicom/article/details/4015064 注意:以下内容为转载,但保留了第一人称,请注意,以免造成不必要的麻烦. 医网联影像工作 ...
- 初识shell脚本
shell字符串.shell数组.shell echo指令.shell test命令.shell if语句.shell case语句.shell for语句.shell while语句.shell b ...
- WPF中的WebBrowser
MainWindow.xaml.cs //新窗口事件 { Uri uri = new Uri(textBox_uri.Text); System.Windows.Forms.Integration.W ...
- JQuery的鼠标滚动事件
jQuery(window).height()代表了当前可见区域的大小,而jQuery(document).height()则代表了整个文档的高度,可视具体情况使用. 注意当浏览器窗口大小改变时(如最 ...
- UILabel 根据内容的多少来计算label的frame
self.label.text = @"...."; 计算 frame 的最新方法 //1.设置lable最大显示行数 self.label.numberOfLines = 0; ...
- 企业应用的Web程序的安全性
提起安全性这个话题,大家恐怕依稀还记得Sony的PSP账户信息泄露的事故造成的重大损失.但是又隐隐觉得这事儿离我很远,无需过多考虑.也有的人会想,我们做的是企业内部系统所以不必太在意.但是,Web程序 ...
- Apache2.2+php5.4在windows上配置实例
这几天一直在win8.1上配置apache+php环境,网上看了很多文章,自己又犯了很多错误才配置成功,对新手来说真是有点小难. 自己打算把配置的详细过程写下来,好帮助其他新手快速配置. 在这里参考了 ...
- NOSQL Mongo入门学习笔记 - MongoDB的安装(一)
手上的工作不是很忙,所以来学习学习很久就像接触的MongoDb,无奈前段时间工作时间都比较多.记录在这里供以后参考 环境: Centos 7 64位 开始: 1. 在官网下载Mongo : wget ...