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.

  Figure A Sample Input of Radar Installations

Input

The input consists of several test cases. The first line of each case contains two integers n (<=n<=) 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


- 

Sample Output

Case :
Case :

Source

贪心。

一开始的思路:

图中ABCD为海岛的位置。假设本题半径为2(符合坐标系),那么A点坐标为(1,1)以此类推。

在题中,记录每个点的坐标,并把一个点新加一个标记变量以标记是否被访问过。

首先以A为圆心,r为半径做圆,交x轴与E1(右)点,做出如图中绿色虚线圆。然后以E1为圆心,半径为r做圆。看此时下一点(B)是否在该圆中。如果在,那么将该点标记变量变为true,再判下一点(C),如果不在那么就新增一个雷达。

后来,换了思路,存储图中红色圆与X轴的交点。

还是原来的贪心思路,仍然先排序,但排序的准则是按红色圆与x轴左交点的先后顺序。

如图,如果B点圆(且如此称呼)的左交点(J1点)在A点圆右交点(E1)左侧,那么B点一定涵盖在A点圆内部。再如图,如果D点圆的左交点(图中未标示)在A点圆的右交点(未标示)右,则D点不在A点圆中。

故,有如下代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<cmath>
using namespace std;
struct Node
{
double left,right;
}p[];
bool cmp(Node a,Node b)
{
return a.left<b.left;
}
int main()
{
int n;
double r;
int ac=;
while(scanf("%d%lf",&n,&r)== && (n || r))
{
int f=;
for(int i=;i<n;i++)
{
double a,b;
scanf("%lf%lf",&a,&b);
if(b>r)
{
f=;
}
else
{
p[i].left=a-sqrt(r*r-b*b);
p[i].right=a+sqrt(r*r-b*b);
}
}
printf("Case %d: ",++ac);
if(f==)
{
printf("-1\n");
continue;
}
sort(p,p+n,cmp);
int ans=;
Node tmp=p[];
for(int i=;i<n;i++)
{
if(p[i].left>tmp.right)
{
ans++;
tmp=p[i];
}
else if(p[i].right<tmp.right)
{
tmp=p[i];
}
}
printf("%d\n",ans); }
return ;
}

poj 1328 Radar Installation(贪心)的更多相关文章

  1. POJ 1328 Radar Installation 贪心 A

    POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...

  2. poj 1328 Radar Installation(贪心+快排)

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

  3. POJ - 1328 Radar Installation(贪心区间选点+小学平面几何)

    Input The input consists of several test cases. The first line of each case contains two integers n ...

  4. POJ 1328 Radar Installation 贪心算法

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

  5. POJ 1328 Radar Installation 贪心 难度:1

    http://poj.org/problem?id=1328 思路: 1.肯定y大于d的情况下答案为-1,其他时候必定有非负整数解 2.x,y同时考虑是较为麻烦的,想办法消掉y,用d^2-y^2获得圆 ...

  6. poj 1328 Radar Installation(贪心)

    题目:http://poj.org/problem?id=1328   题意:建立一个平面坐标,x轴上方是海洋,x轴下方是陆地.在海上有n个小岛,每个小岛看做一个点.然后在x轴上有雷达,雷达能覆盖的范 ...

  7. POJ 1328 Radar Installation 贪心题解

    本题是贪心法题解.只是须要自己观察出规律.这就不easy了,非常easy出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 依照x轴大小排序 2 从最左边的点循环.首先找到最小x轴的圆 ...

  8. POJ 1328 Radar Installation#贪心(坐标几何题)

    (- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<algorithm> #include<cmath ...

  9. 贪心 POJ 1328 Radar Installation

    题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...

  10. poj 1328 Radar Installation (简单的贪心)

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42925   Accepted: 94 ...

随机推荐

  1. HID Boot device.

    整理这篇文章的目的: 客户会有用到遥控器部分(遥控器操作flow:当按下某个键时,MCU会透过UR送command给TP,TP吃到后再透过微软标准的keyboard上报) 要求:在BIOS设定阶段,遥 ...

  2. linux下查阅文件内容cat,more,less,tail

    1.常用cat,直接查看,一次性全部输出 cat  filename cat -b filename 显示行号,除空白行   cat -n 显示行号,包括空白行 常用:cat  filename | ...

  3. Electron开发环境部署

    Electron开发环境部署 安装node.js 可以从node.js官方网站上获取安装包,并进行安装,安装完可以通过 ndoe -v 指令进行版本查看. 本文的开发环境为node.js 4.4.5. ...

  4. iOS避免键盘遮挡输入方案

    项目中经常会遇到这样的问题:一个tableView中有大量的textField,当点击屏幕底部的textfield时,由于键盘弹出挡住了textfield输入框里的内容,造成很差的用户体验,如下图,点 ...

  5. 网页CSS

    CSS 样式表,(分三类:内联.内嵌.外部) 1,内联, 直接作于于 元素 例:   <p style="font-size:14px;"> 2,内嵌 作用于网页 首先 ...

  6. 使用Gird++打印出现“Retrieving the COM class factory for component with CLSID”的解决办法

    我们的接口需要返回一个gird++生成PDF文件的二进制数据,在本地测试都很好,发布到服务器上一直出现“Retrieving the COM class factory for component w ...

  7. Linux下的静态IP配置【weber出品】

    配置Linux下的静态IP地址 因为服务器的IP地址是固定的,不像我们自己家的笔记本的IP是动态的.所以我们要将这个地址给写成静态的. 直接编辑这个这个配置文件即可: vi /etc/sysconfi ...

  8. MySql不支持主外键

    创建表不支持主外键,能够添加外键成功,但是无法外键约束.查资料发现MySql的默认ENGINE 为MyISAM  ,不支持外键,需要修改为 INNODB 修改前: Create Table CREAT ...

  9. MongoDB自学笔记2---1.2 初识MongoDB

    1.2.1MongoDB简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之 ...

  10. LLVM对注释的新增支持 @ WWDC 2013

    很久之前我就在想:“我应该按照什么格式写注释,才能像Apple官方API那样按住Option键并点击函数名可以跳出文档说明”,如下图: 我理所当然地认为这个功能应该是根据现有注释的格式来进行排版的,于 ...