【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,一经查实,立即删除!

相关文章

sql语句实现分页查询

2005及以上版本 -- 分页查询&#xff08;通用型&#xff09; select top pageSize * from (select row_number() over(order by sno asc) as rownumber,* from student) temp_row where rownumber>((pageIndex-1)*pageSize);2012及以上版本 select * from student orde…

【51nod-1289】大鱼吃小鱼

题干&#xff1a; 有N条鱼每条鱼的位置及大小均不同&#xff0c;他们沿着X轴游动&#xff0c;有的向左&#xff0c;有的向右。游动的速度是一样的&#xff0c;两条鱼相遇大鱼会吃掉小鱼。从左到右给出每条鱼的大小和游动的方向&#xff08;0表示向左&#xff0c;1表示向右&…

如何通过属性给实体赋值

获取实体属性 Type type family.GetType(); //family为实体对象 PropertyInfo[] infos type.GetProperties(); foreach (PropertyInfo info in infos){info.GetValue(family); //取值info.SetValue(bFamily, info.GetValue(family)); //赋值 }

【CF#801 A.】 Vicious Keyboard(字符串查找,水题)

题干&#xff1a;Tonio has a keyboard with only two letters, "V" and "K". One day, he has typed out a string s with only these two letters. He really likes it when the string "VK" appears, so he wishes to change at most one let…

关于ajax请求400问题解决

ajax请求&#xff1a;400 bad request 原因有两种&#xff1a; 参数不匹配&#xff0c;请求太长 如何解决 针对参数不匹配&#xff0c;只能一一对照 请求太长的话&#xff0c;api最好使用post方式请求&#xff0c;我遇到的问题就是post请求太长&#xff0c;这时候需要给参数…

【uva-673】 Parentheses Balance(括号匹配问题)

题干&#xff1a; You are given a string consisting of parentheses () and []. A string of this type is said to be correct:(a)if it is the empty string(b)if A and B are correct, AB is correct,(c)if A is correct, (A) and [A] is correct.Write a program that ta…

layui前端框架弹出框图标整理

1绿色对勾 2红色 3黄色问号 4黑色小锁 5红色哭脸 6绿色笑脸

【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…

layui 折叠面板使用无效问题

静态折叠面板无法使用 解决方案&#xff1a;layui.js 引用位置应该放在距离layui.use临近的地方 动态设置折叠面板无法点击折叠展开 解决方案&#xff1a;动态加载完界面后&#xff0c;执行下面语句&#xff1a; layui.element.init();

【HDU - 1031 】Design T-Shirt(水题 排序)

题干&#xff1a;Soon after he decided to design a T-shirt for our Algorithm Board on Free-City BBS, XKA found that he was trapped by all kinds of suggestions from everyone on the board. It is indeed a mission-impossible to have everybody perfectly satisfie…

针对标签属性data-**的使用

在网站开发过程中&#xff0c;很多时候需要自定义一些标签属性&#xff0c;可以使用&#xff1a; 在html中&#xff1a; data-**"" 在js代码中取值&#xff1a; var **标签.data("**")

【HDU - 2203】 亲和串 (思维题,可选KMP)

题干&#xff1a;Problem Description人随着岁数的增长是越大越聪明还是越大越笨&#xff0c;这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考&#xff0c;因为他在很小的时候就知道亲和串如何判断了&#xff0c;但是发现&#xff0c;现在长大了却不知道怎么去…

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

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

layui中日期格式化方式

如何对日期进行格式化 layui.util.toDateString(日期值, yyyy-MM-dd HH:mm:ss.fff)

【51NOD - 1523】 非回文(dfs)

题干&#xff1a;一个字符串是非回文的&#xff0c;当且仅当&#xff0c;他只由前p个小写字母构成&#xff0c;而且他不包含长度大于等于2的回文子串。 给出长度为n的非回文串s。请找出字典序比s大的&#xff0c;而且字典序要最小的长度为n的非回文。 Input单组测试数据。 第一…

C# XML字符串与DataTable相互转换

不多说&#xff0c;直接上代码&#xff1a; //DataTable转Xml字符串 public static string ConvertDataTableToXML(DataTable xmlDS){MemoryStream stream null;XmlTextWriter writer null;try{stream new MemoryStream();writer new XmlTextWriter(stream, Encoding.Defa…

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

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

C# 实现将网络资源保存到本地

/// <summary>/// 单个文件保存从对方服务器到自己网站/// </summary>/// <param name"aUrl"></param>/// <param name"aPath"></param>/// <returns></returns>[HttpGet]public string FileSave(string …

【sdut 1751】 区间覆盖问题

区间覆盖问题Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Description设x1 , x2 ,…… , xn 是实直线上的n 个点。用固定长度的闭区间覆盖这n 个点&#xff0c;至少需要多少个这样的固定长度闭区间?对于给定的实直线上的n个点和闭区间的长…

C# 读取根目录的json文件中的某个值

/// <summary>/// 读取JSON文件/// </summary>/// <param name"key">JSON文件中的key值</param>/// <returns>JSON文件中的value值</returns>public string Readjson(string key){//获取winform应用根目录string jsonfile Appl…