原题链接

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

解题思路

  1. 如果海岛的y坐标大于雷达的半径,那么一定无解。
  2. 如果有解,那么将每个海岛的左右极限求出来,然后按照左端点从小到大排序,依次遍历,如果当前海岛的左端点在当前雷达的右端点之前,那么说明该雷达可以检测到该海岛,并且雷达右端点要更新为当前海岛的右端点和当前雷达的右端点的最小值;如果当前海岛的左端点超过当前雷达的右端点,那么该雷达肯定检测不到该海岛,那么就要加一个雷达,并且右端点为该海岛的右端点。即:
        for(int i = 1; i < n; i++){if( a[i].first <= r){r = min(r, a[i].second);continue;}else{sum++;r = a[i].second;}}

坑点

  1. 输出的时候忘了格式化了,每个输出都写成了Case 1:
  2. 如果无解,输出-1的时候也得写Case
  3. 如果if( a[i].first <= r),忘记更新右端点了,需要将右端点更新为海岛和雷达的最小值。
  4. 左右端点要用double,因为开方之后可能为小数。

AC代码

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
using namespace std;
int n, d, x, y;
typedef pair <double, double> p;
p a[1005];
bool cmp(p a, p b)
{return a.first < b.first;
}
int main(void)
{int t = 0;while(cin >> n >> d){if(n == 0 && d == 0)break;t++;int flag = 1;for(int i = 0; i < n; i++){cin >> x >> y;if(y > d){flag = 0;}a[i].first = x - sqrt(d  * d - y * y);a[i].second = x + sqrt(d  * d - y * y);}if(flag == 0){printf("Case %d: -1\n", t);continue;}sort(a, a + n, cmp);int sum = 1;double r = a[0].second;for(int i = 1; i < n; i++){if( a[i].first <= r){r = min(r, a[i].second);continue;}else{sum++;r = a[i].second;}}printf("Case %d: %d\n", t, sum);}return 0;
}

【POJ】Radar Installation题解相关推荐

  1. POJ Radar Installation 1328(雷达)贪心算法

    问题描述 问题链接 Description Assume the coasting is an infinite straight line. Land is in one side of coast ...

  2. 【贪心】Radar Installation(poj 1328)

    Radar Installation poj 1328 题目大意: 在平面直角坐标系的一二象限上有n个小岛,现在让你在x坐标上布置雷达,每个雷达可以侦测以它为原心,半径为m的圆内的所有小岛,现在问侦测 ...

  3. 【POJ - 1328】Radar Installation(贪心+计算几何)安装雷达辐射岛屿

    题干: Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the ot ...

  4. POJ 1012 Joseph 题解

    POJ 1012 Joseph 题解 题目解读: The Joseph's problem is notoriously known. For those who are not familiar w ...

  5. poj 1328 Radar Installation

    题目链接:http://poj.org/problem?id=1328 题意: 设x轴为海岸,下方为陆地,上方为海.海上有n个岛屿,现在需要用雷达将所有的岛屿覆盖起来.给定岛屿个数及每个岛屿的坐标,给 ...

  6. POJ 1328 Radar Installation【贪心】

    POJ 1328 题意: 将一条海岸线看成X轴,X轴上面是大海,海上有若干岛屿,给出雷达的覆盖半径和岛屿的位置,要求在海岸线上建雷达,在雷达能够覆盖全部岛屿情况下,求雷达的最少使用量. 分析: 贪心法 ...

  7. POJ - 1328 Radar Installation(贪心+思维)

    题目链接:点击查看 题目大意:校长想通过监控设备覆盖学校内的N座建筑物,每座建筑物被视作一个质点,在笛卡尔坐标系中给出他们的坐标(x,y),并且所有建筑物均处在x轴的上方.因为学校的供电和传输线路均沿 ...

  8. POJ 3279(Fliptile)题解

    以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...

  9. Radar Installation(贪心,sort)

    在poj上C++可以AC,但G++不行.杭电上更是好多的TLE,结果把cin改成scanf便可以轻松AC. #include <iostream> #include <algorit ...

  10. Radar Installation

    题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=27586 题意: 在海岸线上摆放雷达并限定雷达覆盖半径d,再以 ...

最新文章

  1. Linux命令find的35个实例
  2. 【Qt】使用sqlite3数据库时,主键自增和获取自增后的主键的
  3. signavio-core-components ant build-all-in-one-war failed
  4. Hyperledger Fabric 交易流程
  5. SHOI2016 黑暗前的幻想乡
  6. Spring Boot的自动化配置原理
  7. 担当大任者的九大特征
  8. [CodeForces-1138B] *Circus 解方程|数学
  9. python自动化_python自动化办公?学这些就够用了
  10. 让你更好的使用jQuery插件
  11. smack android 示例代码,android客户端xmpp smack openfire简单开发实例
  12. 【观察】 全新紫光云:“云数智”底盘再升级,背后的三重新价值
  13. 关于Document类型的总结
  14. 【桧木】桧木精油的功效 台湾桧木价值所在
  15. cmd脚本win10使用schtasks命令实现定时任务
  16. css_radius-corner语法分析
  17. 微信小程序开发架构——JavaScript的基本概述 和 JavaScript在 Nodejs、小程序中、浏览器中的使用方法
  18. Kafka能作为数据库使用吗
  19. ubuntu机械盘写入cannot be copied because you do not have permissions to create it in the destination.
  20. halcon中面到面的距离_halcon学习网

热门文章

  1. 收藏|史上最全的30个生物实验技术及原理
  2. C语言 —— char类型的使用(二)
  3. Java JNI调用kaldi动态链接库(Linux版本)
  4. 使用 OpenSSL 创建ssl证书
  5. 淘宝app搜索排名优化技巧
  6. canvas绘制动态图片
  7. ios 扫描本地音乐_iOS如何获取本地的音乐歌曲mp3的信息数据
  8. 沉迷于网络的人数_一个沉迷于追求的搜索者
  9. Unity3d开发wp8问题汇总
  10. 龙ol服务器维护补偿boss,龙OL低级稀有BOSS刷新点