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, so an island in the sea can be covered by a radius installation, if the distance between them is at most d.

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 consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases.

The input is terminated by a line containing pair of zeros

Output

For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.

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的更多相关文章

  1. [POJ1328]Radar Installation

    [POJ1328]Radar Installation 试题描述 Assume the coasting is an infinite straight line. Land is in one si ...

  2. POJ--1328 Radar Installation(贪心 排序)

    题目:Radar Installation 对于x轴上方的每个建筑 可以计算出x轴上一段区间可以包含这个点 所以就转化成 有多少个区间可以涵盖这所有的点 排序之后贪心一下就ok 用cin 好像一直t看 ...

  3. POJ1328 Radar Installation 【贪心&#183;区间选点】

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54593   Accepted: 12 ...

  4. POJ1328 Radar Installation 解题报告

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  5. poj1328 Radar Installation(贪心 策略要选好)

    https://vjudge.net/problem/POJ-1328 贪心策略选错了恐怕就完了吧.. 一开始单纯地把island排序,然后想从左到右不断更新,其实这是错的...因为空中是个圆弧. 后 ...

  6. POJ1328 Radar Installation(贪心)

    题目链接. 题意: 给定一坐标系,要求将所有 x轴 上面的所有点,用圆心在 x轴, 半径为 d 的圆盖住.求最少使用圆的数量. 分析: 贪心. 首先把所有点 x 坐标排序, 对于每一个点,求出能够满足 ...

  7. zoj1360/poj1328 Radar Installation(贪心)

    对每个岛屿,能覆盖它的雷达位于线段[x-sqrt(d*d-y*y),x+sqrt(d*d+y*y)],那么把每个岛屿对应的线段求出来后,其实就转化成了经典的贪心法案例:区间选点问题.数轴上有n个闭区间 ...

  8. poj1328 Radar Installation —— 贪心

    题目链接:http://poj.org/problem?id=1328 题解:区间选点类的题目,求用最少的点以使得每个范围都有点存在.以每个点为圆心,r0为半径,作圆.在x轴上的弦即为雷达可放置的范围 ...

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

    ZOJ地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=360 POJ地址:http://poj.org/problem?id ...

随机推荐

  1. OCI-DML-更新数据库中不存在的字段

    用gtest来测试oracle中oci方式的SQL语句操作,在测试update数据库中不存在的异常案例的时候,日志没有报错,但是结束后跳出了数据库连接 gtest也没有给出正常的结果,本身update ...

  2. 基于MRG_MyISAM引擎的Mysql分表

    正常情况下的分表,都是直接创建多个相同结构的表,比如table_1.table_2...最近碰到一个特殊需求,需要创建一个主表,所有分表的数据增删改查,全部自动实时更新到主表,这个时候可以使用MRG_ ...

  3. js给数字加三位一逗号间隔的两种方法(面试题)

    方法一:   <script type= "text/javascript"> //保留三位小数,toLocaleString() 方法可把一个 Number 对象转换 ...

  4. ubuntu中替代visio的软件 dia

    ubuntu 中 软件  dia 可以替代 ms-visio软件. 安装过程可以在线安装: sudo apt-get install dia

  5. CentOS安装,更新Python

    1.查看当前Python版本 python -V 2.查看当前CentOS版本 cat /etc/redhat-release 3.安装所有的开发工具包 yum groupinstall " ...

  6. .NET基础之迭代器

    使用foreach循环是有IEnumerator接口来实现的,IEnumerator即实现了迭代器,在foreach中如何迭代一个集合arrayList呢? 调用arrayLis.GetEnumber ...

  7. 生成XML文件,通过实体生成XML文件

    实体 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xm ...

  8. PHP数组的操作

    一.数组操作的基本函数数组的键名和值array_values($arr);获得数组的值array_keys($arr);获得数组的键名array_flip($arr);数组中的值与键名互换(如果有重复 ...

  9. iOS 通览(二)

    一.关键词 extern:C语言的函数外部声明. 如果你要在一个.c或者.m中使用另外一个.c文件的函数的话,需要在文件中写入目标函数的外部引用的声明. 二.自定义View 自定义View添加控件对象 ...

  10. shell 实现word count

    awk '{arr[$2]+=$1}END{for (i in arr) print i,arr[i]}' sort_all.txt | sort -k2nr -g