【POJ】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
解题思路
- 如果海岛的y坐标大于雷达的半径,那么一定无解。
- 如果有解,那么将每个海岛的左右极限求出来,然后按照左端点从小到大排序,依次遍历,如果当前海岛的左端点在当前雷达的右端点之前,那么说明该雷达可以检测到该海岛,并且雷达右端点要更新为当前海岛的右端点和当前雷达的右端点的最小值;如果当前海岛的左端点超过当前雷达的右端点,那么该雷达肯定检测不到该海岛,那么就要加一个雷达,并且右端点为该海岛的右端点。即:
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;}}
坑点
- 输出的时候忘了格式化了,每个输出都写成了Case 1:
- 如果无解,输出-1的时候也得写Case
- 如果
if( a[i].first <= r)
,忘记更新右端点了,需要将右端点更新为海岛和雷达的最小值。 - 左右端点要用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题解相关推荐
- POJ Radar Installation 1328(雷达)贪心算法
问题描述 问题链接 Description Assume the coasting is an infinite straight line. Land is in one side of coast ...
- 【贪心】Radar Installation(poj 1328)
Radar Installation poj 1328 题目大意: 在平面直角坐标系的一二象限上有n个小岛,现在让你在x坐标上布置雷达,每个雷达可以侦测以它为原心,半径为m的圆内的所有小岛,现在问侦测 ...
- 【POJ - 1328】Radar Installation(贪心+计算几何)安装雷达辐射岛屿
题干: Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the ot ...
- POJ 1012 Joseph 题解
POJ 1012 Joseph 题解 题目解读: The Joseph's problem is notoriously known. For those who are not familiar w ...
- poj 1328 Radar Installation
题目链接:http://poj.org/problem?id=1328 题意: 设x轴为海岸,下方为陆地,上方为海.海上有n个岛屿,现在需要用雷达将所有的岛屿覆盖起来.给定岛屿个数及每个岛屿的坐标,给 ...
- POJ 1328 Radar Installation【贪心】
POJ 1328 题意: 将一条海岸线看成X轴,X轴上面是大海,海上有若干岛屿,给出雷达的覆盖半径和岛屿的位置,要求在海岸线上建雷达,在雷达能够覆盖全部岛屿情况下,求雷达的最少使用量. 分析: 贪心法 ...
- POJ - 1328 Radar Installation(贪心+思维)
题目链接:点击查看 题目大意:校长想通过监控设备覆盖学校内的N座建筑物,每座建筑物被视作一个质点,在笛卡尔坐标系中给出他们的坐标(x,y),并且所有建筑物均处在x轴的上方.因为学校的供电和传输线路均沿 ...
- POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
- Radar Installation(贪心,sort)
在poj上C++可以AC,但G++不行.杭电上更是好多的TLE,结果把cin改成scanf便可以轻松AC. #include <iostream> #include <algorit ...
- Radar Installation
题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=27586 题意: 在海岸线上摆放雷达并限定雷达覆盖半径d,再以 ...
最新文章
- Linux命令find的35个实例
- 【Qt】使用sqlite3数据库时,主键自增和获取自增后的主键的
- signavio-core-components ant build-all-in-one-war failed
- Hyperledger Fabric 交易流程
- SHOI2016 黑暗前的幻想乡
- Spring Boot的自动化配置原理
- 担当大任者的九大特征
- [CodeForces-1138B] *Circus 解方程|数学
- python自动化_python自动化办公?学这些就够用了
- 让你更好的使用jQuery插件
- smack android 示例代码,android客户端xmpp smack openfire简单开发实例
- 【观察】 全新紫光云:“云数智”底盘再升级,背后的三重新价值
- 关于Document类型的总结
- 【桧木】桧木精油的功效 台湾桧木价值所在
- cmd脚本win10使用schtasks命令实现定时任务
- css_radius-corner语法分析
- 微信小程序开发架构——JavaScript的基本概述 和 JavaScript在 Nodejs、小程序中、浏览器中的使用方法
- Kafka能作为数据库使用吗
- ubuntu机械盘写入cannot be copied because you do not have permissions to create it in the destination.
- halcon中面到面的距离_halcon学习网