POJ1328-Radar Installation
描述:
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
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
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.
代码:
每一个海岛可以在产生一个圆心的范围,在该范围内的任意雷达都可以覆盖到该点。要求雷达的最少的数目,采用贪心的思路,就是使选择的圆心能够尽量的属于更多的圆心的区间。如果雷达范围无法到达某个点,则无解。
边输入边处理的时候,不能break。就因为这个RE了无数次(╯‵□′)╯︵┴─┴
#include <cmath> #include <cstdlib> #include <iostream> using namespace std; typedef struct{double left;double right; }node; node a[1005]; int cmp(const void *a, const void *b){return (*(node*)a).left >= (*(node*)b).left ? 1 : -1; } int main(){int tc=1,n,d,flag,count,x,y;double delta,t;while( scanf("%d%d",&n,&d)!=EOF ){if( n==0 && d==0 ) break;flag=1;if( d<=0 ) flag=0;for( int i=0;i<n;i++ ){scanf("%d%d",&x,&y);if( y<=d ){//岛屿在雷达范围delta=sqrt((double)(d*d-y*y));a[i].left=x-delta;//得到区间a[i].right=x+delta;}else{flag=0;//这里不能写break,因为输入还未结束 }}if( flag==0 )//无解printf("Case %d: -1\n",tc);else{qsort(a,n,sizeof(node),cmp); t=a[0].right;count=1;for( int i=1;i<n;i++ ){if( a[i].left>t ){//当前区间左界大于相交区间的最右count++;t=a[i].right;//更新相交右界 }else{if( a[i].right<t )//取相交的区间t=a[i].right;//更新相交区间右界 }}printf("Case %d: %d\n",tc,count);}tc++;}system("pause");return 0; }
转载于:https://www.cnblogs.com/lucio_yz/p/4771828.html
POJ1328-Radar Installation相关推荐
- POJ-1328 Radar Installation 贪心
以每个点算出左右覆盖的雷达所在x轴范围,然后贪心计算出所需圆的个数. 当后一个点的圆心在x轴的左坐标在前一个点的右坐标的右坐标之前,则这个点就会被覆盖. 代码如下:(C++能过,G++ runtime ...
- 【贪心】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 Radar Installation 1328(雷达)贪心算法
问题描述 问题链接 Description Assume the coasting is an infinite straight line. Land is in one side of coast ...
- 【POJ1328】Radar Installation(贪心,决策包容)
problem 平面直角坐标系上有n个点. 在x轴上找尽量少的点,并以这些点为圆心画一个半径为d的圆,使得给定的点都在画出来的圆里. 求最少要画的点数,如果不能输出-1. solution 一.不能的 ...
- poj 1328 Radar Installation
题目链接:http://poj.org/problem?id=1328 题意: 设x轴为海岸,下方为陆地,上方为海.海上有n个岛屿,现在需要用雷达将所有的岛屿覆盖起来.给定岛屿个数及每个岛屿的坐标,给 ...
- Radar Installation(贪心,sort)
在poj上C++可以AC,但G++不行.杭电上更是好多的TLE,结果把cin改成scanf便可以轻松AC. #include <iostream> #include <algorit ...
- POJ - 1328 Radar Installation(贪心+思维)
题目链接:点击查看 题目大意:校长想通过监控设备覆盖学校内的N座建筑物,每座建筑物被视作一个质点,在笛卡尔坐标系中给出他们的坐标(x,y),并且所有建筑物均处在x轴的上方.因为学校的供电和传输线路均沿 ...
- Radar Installation
题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=27586 题意: 在海岸线上摆放雷达并限定雷达覆盖半径d,再以 ...
- POJ 1328 Radar Installation【贪心】
POJ 1328 题意: 将一条海岸线看成X轴,X轴上面是大海,海上有若干岛屿,给出雷达的覆盖半径和岛屿的位置,要求在海岸线上建雷达,在雷达能够覆盖全部岛屿情况下,求雷达的最少使用量. 分析: 贪心法 ...
最新文章
- 百度不到的硬核资源,8h删,抓紧收藏!
- Algs4-2.2.29自然的归并排序(未解决)
- Linux显示某文件中有关某字符串的信息
- js es6 reduce用法示例:实现数据累加
- 小猿圈分享使用Java模拟三道门的游戏
- C++编程(一):匈牙利命名法
- pc端无法ping android模拟器_【内附下载方式】PC端最新宝可梦 Lets Go去皮去伊模拟器+最新dlc+mod...
- php如何判断是否关注,php如何判断用户是否关注微信公众号
- MySQL 8.0 ROLE管理
- jsp+servlet+jdbc实现对数据库的增删改查
- 高一下学期计算机考试知识点,高中数学知识点总结
- 亚泰盛世携NB物理实验邀你莅临第66届中国教育装备展
- RADIUS协议基础原理
- 等额本息房贷月供计算
- 钉钉实时拍照和上传图片
- 易语言注册机接码平台对接
- Excel导出带有多个图表(柱状图、折线图、饼状图等)
- 项目4:后台管理的开发和使用(前端)
- flex LCDS 整合 java 增删改查
- 计算机专业是武大冷门吗,武汉大学最不好的专业是什么?附武汉大学的弱势专业名单...
热门文章
- python 内存溢出能捕获吗_从0基础学习Python (19)[面向对象开发过程中的异常(捕获异常~相关)]...
- tfs java开发需要jar_使用Spring框架开发最少需要哪些jar包,依赖jar包有哪些?
- 远程导入mysql_mysql导入sql文件命令和mysql远程登陆使用详解
- mysql 加索引不起作用_mysql加索引及索引失效的情况
- Go Mysql Driver 集成 Seata-Golang 解决分布式事务问题
- Knative 实战:基于 Knative Serverless 技术实现天气服务-下篇
- Mysql小练习(1)
- oracle z中rowid,如何将索引块里面的rowid和表里面的rowid对应起来?
- 您不是订单管理的定向开发者_Web Summit 2020大会:华为在欧洲发布HMS Connect,持续助力合作伙伴与开发者的创新增长...
- Python:Django开发函数笔记: