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

题干:

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 (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 11 2
0 20 0
Sample Output
Case 1: 2
Case 2: 1


题目大意:

    在x轴上建造雷达,似的x轴上方的所有岛屿都能被辐射到。每个雷达的辐射半径已知,求:为使所有岛屿都被辐射到,所需要的最少雷达个数。

解题报告:

    这应该说是我遇到的第二个不算很水的计算几何问题,上一个是 解救雅典娜,用dp做,这一题是用贪心做。先按照x坐标从小到大排序,先确定一个半完成值(half-finish自己取的名,第一次使用是在这里)圆心setx,然后逐个遍历圆心curx(这里取右圆心)(因为圆的半径已知为d,所以圆心的x轴坐标显然可求),如果curx<=setx 显然要取curx 为圆心的那个圆,因为这样两个岛屿都会被包进去。如果curx>setx 那就要比较一下该点到圆心setx的距离是否大于d,如果小于等于d,那虚惊一场可以辐射到这个岛,如果大于d,那凉凉,只能新建一个雷达了所以ans++。遍历完所有岛屿即可得出答案ans。(但是总觉得不严谨,因为你虽然这个点(记为A)到圆心(记为setx1)的距离大于d了,你就更新了setx(更新为setx2),但有可能下一个点(记为B)满足curx>setx1&&到圆心的距离<=d呢?,所以不应该这么着急着更新setx吧,,,因为你不确定B是不是也不满足小于等于d这个要求啊)

对上面问题的回答:是这样的,确实保证不了,但是因为你已经有一个A满足不了了,所以无论如何你也要为了A岛屿而建立一个雷达,而这个新雷达能否包含B就看B这个的坐标了。。。即:题目只问的是最少需要建几个雷达,而非让你把每个岛屿属于哪个雷达说清楚,因此没必要考虑这些问题。


ac代码:

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;struct Node {int x, y;
} node[1000 + 5];bool cmp(const Node & a,const Node & b) {return a.x<b.x;//此题y坐标排不排序无所谓的、、、	}int main()
{int n,d;int ans=1;int maxy;int iCase=0;double curx,setx,seti;//curx表示当前尝试的右圆心  setx表示当前确定的右圆心  seti表示当前确定的索引值 double curd;//curd表示当前尝试的圆的半径 while(scanf("%d %d",&n,&d)) {ans=1;maxy=0;//别忘了初始化! ! if(n==0 && d==0) break;for(int i = 0; i<n; i++) {scanf("%d %d",&node[i].x,&node[i].y);	maxy=max(node[i].y,maxy);}if(maxy>d) {printf("Case %d: -1\n" , ++iCase);continue;}sort(node,node+n,cmp);curx=node[0].x+sqrt(d*d-( node[0].y )*( node[0].y ) );setx=curx;seti=0;for(int i = 1; i<n; i++) {//因为第一个已经初始化好了,所以下标从1开始 curx=node[i].x+sqrt(d*d-( node[i].y )*( node[i].y ) );if(curx<=setx) {setx=curx;}else {curd=sqrt( (node[i].x-setx)*(node[i].x-setx) +(node[i].y)*(node[i].y) );if(curd<=d) {continue;}else {ans++;setx=curx;}}}	printf("Case %d: %d\n",++iCase,ans);} return 0 ;
}

下面附上kuangbin大佬的代码:(其实差不多的只不过我是用的点到setx的距离d,他是用的点的左圆心)

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<algorithm>//sort所在的库文件,排序用
using namespace std;
const int MAXN=1005;
struct Line
{double l,r;
}line[MAXN];//每个岛作半径为d的圆,与x轴所截的线段bool cmp(Line a,Line b)
{return a.l<b.l;
}       //按照线段的左端点从小到大排序
int main()
{//freopen("test.in","r",stdin);//freopen("test.out","w",stdout);int n,d;int i;int x,y;bool yes;//确定是不是有解int icase=1;while(cin>>n>>d){yes=true;int cnt=0;if(n==0&&d==0)break;for(i=0;i<n;i++){cin>>x>>y;if(yes==false)continue;if(y>d)yes=false;else{line[i].l=(double)x-sqrt((double)d*d-y*y);line[i].r=(double)x+sqrt((double)d*d-y*y);}    }if(yes==false){cout<<"Case "<<icase++<<": -1"<<endl;continue;}sort(line,line+n,cmp);cnt++;double now=line[0].r;for(i=1;i<n;i++){if(line[i].r<now)//这点很重要 now=line[i].r;else if(now<line[i].l){now=line[i].r;cnt++;}    }cout<<"Case "<<icase++<<": "<<cnt<<endl;    }     return 0;
}




本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442150.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【CF#757A】Gotta Catch Em' All!

题干&#xff1a;Bash wants to become a Pokemon master one day. Although he liked a lot of Pokemon, he has always been fascinated by Bulbasaur the most. Soon, things started getting serious and his fascination turned into an obsession. Since he is too young…

【HDU - 4509】湫湫系列故事——减肥记II(合并区间模板 or 离散化标记 or 线段树)

题干&#xff1a;虽然制定了减肥食谱&#xff0c;但是湫湫显然克制不住吃货的本能&#xff0c;根本没有按照食谱行动&#xff01; 于是&#xff0c;结果显而易见… 但是没有什么能难倒高智商美女湫湫的&#xff0c;她决定另寻对策——吃没关系&#xff0c;咱吃进去再运动运动消…

【HDU - 2093】 考试排名(排序+格式输出)

题干&#xff1a;C编程考试使用的实时提交系统&#xff0c;具有即时获得成绩排名的特点。它的功能是怎么实现的呢&#xff1f; 我们做好了题目的解答&#xff0c;提交之后&#xff0c;要么“AC”&#xff0c;要么错误&#xff0c;不管怎样错法&#xff0c;总是给你记上一笔&…

.net core 5 IIS Api网站部署需要注意(同.net 6)

应用程序池&#xff1a;.net clr 版本&#xff1a;无托管代码 2.安装.NET Core SDK和AspNetCoreModule托管模块 此工具要在官网直接下载即可

【HDU - 2087】 剪花布条(直接模拟 or KMP)

题干&#xff1a; 一块花布条&#xff0c;里面有些图案&#xff0c;另有一块直接可用的小饰条&#xff0c;里面也有一些图案。对于给定的花布条和小饰条&#xff0c;计算一下能从花布条中尽可能剪出几块小饰条来呢&#xff1f; Input输入中含有一些数据&#xff0c;分别是成…

webapi自宿主设置本地端口使用https协议

首先&#xff0c;你要申请证书&#xff0c;然后导入到证书里面&#xff1a; 具体步骤&#xff1a;运行–MMC命令&#xff0c;进入如下界面进行设置&#xff1a; 一直点下一步直到完成&#xff0c;然后将证书导入到个人里面 这个时候进入cmd程序运行如下命令&#xff1a; /…

第九届(2018)蓝桥杯 山东省赛解题报告(题目+分析+代码)

1标题&#xff1a;第几天 2000年的1月1日&#xff0c;是那一年的第1天。那么&#xff0c;2000年的5月4日&#xff0c;是那一年的第几天&#xff1f; 注意&#xff1a;需要提交的是一个整数&#xff0c;不要填写任何多余内容。 【答案】&#xff1a;125 2标题&#xff1a;明码 …

Windows下 Python3.7.0 运行环境的搭建 一套操作后就可以使用Python写代码啦~

1.下载Python for windows 废话不说&#xff0c;直接上网址&#xff1a;https://www.python.org/ftp/python/3.5.1/python-3.5.1.exe 2.安装Python for windows 运行安装文件之后&#xff0c;你会看到这个页面&#xff1a;不得不说Python 在 Windows平台下的安装比傻瓜式还傻瓜…

【HDU - 1326】Box of Bricks(模拟水题)

题干&#xff1a;Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. Look, Ive built a wall!, he tells his older sister Alice. Nah, you should make all stacks the same height. Then you …

微信公众平台网站开发JS_SDK遇到的bug——wx.config注册提示成功,但部分接口注册失败问题

1 2022-02-23 使用微信公众平台调用扫一扫接口&#xff0c;总是注册不成功 这是进行注册后成功注册的接口提示 尝试注册了以下接口 拥有相关权限 解决办法&#xff1a;猜测失败原因为&#xff1a;子界面进行注册&#xff0c;在父界面进行注册后&#xff0c;成功

c++ 低位在前 高位在后_A股市场:如果股票涨停后第二天“高开低走”,你知道怎么操作才能利益最大化吗?...

(本文由公众号越声策略(yslc188)整理&#xff0c;仅供参考&#xff0c;不构成操作建议。如自行操作&#xff0c;注意仓位控制和风险自负。)如果你的股票涨停后第二天高开低走&#xff0c; 后市怎么操作&#xff1f;简单来讲&#xff0c;高开低走就是开盘价高于上个交易日的收盘…

文件内容查找方式

第一种&#xff0c;使用windows自带的查找工具 搜索工具里面有”高级选项“&#xff0c;选择”文件内容“然后进行搜索即可 第二种&#xff0c;使用命令行 在需要进行搜索的文件夹下使用命令行&#xff1a; Get-ChildItem -Path F:\ -Recurse | Select-String -Pattern &qu…

【HDU - 1301】Jungle Roads(并查集+最小生成树)(内附最小生成树两种算法 克鲁斯特尔算法amp;amp;普里姆算法)

题干&#xff1a; Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5505 Accepted Submission(s): 3976 Problem Description The Head Elder of the tropical island of Lagrishan has a proble…

vscode中怎样格式化js代码_[VSCode插件推荐] Bracket Pair Colorizer: 为代码中的括号添上一抹亮色...

在代码编写过程中&#xff0c;各种括号 {[()]} 必不可少。然而&#xff0c;随着代码量的增加&#xff0c;你有没有因为括号的嵌套太多&#xff0c;而导致代码难以阅读&#xff1f;我们来看看下面的代码&#xff0c;在第三行代码的最后部分&#xff0c;连续出现了5个右括号&…

*【HDU - 1506】【POJ - 2559】Largest Rectangle in a Histogram(单调栈或动态规划)

题干&#xff1a; Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consist…

【基础知识】大数据组件HBase简述

HBase是一个开源的、面向列&#xff08;Column-Oriented&#xff09;、适合存储海量非结构化数据或半结构化数据的、具备高可靠性、高性能、可灵活扩展伸缩的、支持实时数据读写的分布式存储系统。 只是面向列&#xff0c;不是列式存储 mysql vs hbase vs clickhouse HMaster …

改变定时器获取传感器频度_广东梅州梅县压力传感器*校对

广东梅州梅县压力传感器*校对看门狗寄存器不会改变或改变不大&#xff0c;如果看门狗寄存器发生了改变或改变很大&#xff0c;则说明系统陷入“死循环”.需要进行出错处理。在工业应用中&#xff0c;严重的干扰有时会破坏中断方式控制字&#xff0c;关闭中断&#xff0c;造成看…

springboot 不响应字段为空_面试官扎心一问:Tomcat 在 SpringBoot 中是如何启动的?...

作者&#xff1a;木木匠 http://my.oschina.net/luozhou/blog/3088908前言我们知道 SpringBoot 给我们带来了一个全新的开发体验&#xff0c;我们可以直接把 web 程序达成 jar 包&#xff0c;直接启动&#xff0c;这就得益于 SpringBoot 内置了容器&#xff0c;可以直接启动&am…

a1708硬盘转接口_资讯:希捷上架新款银河Exos系列机械硬盘,15000转+SAS协议

今日最新消息&#xff0c;希捷上架一款新品希捷银河Exos系列机械硬盘。据悉这款硬盘采用了SAS协议&#xff0c;转速高达15000RPM&#xff0c;目前公布的售价600GB为1899元RMB。据官方介绍这款希捷银河Exos系列机械硬盘为2.5英寸&#xff0c;15mm的厚度&#xff0c;最高的转速可…

腐蚀单机怎么进_暖气片堵塞是什么原因?要怎么解决呢?

你知道散热器到底为什么堵塞吗&#xff1f;散热器堵塞了怎么办&#xff1f;下面和金旗舰散热器小编一起来看看吧~一、散热器堵塞怎么办首先&#xff0c;把进回水阀先全部关闭&#xff0c;用扳手将散热器的堵头轻轻拧开。这里需要注意的是&#xff0c;堵头对应的散热器下面要放一…