Radar Installation
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 42925   Accepted: 9485

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.



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

  1. 3 2
  2. 1 2
  3. -3 1
  4. 2 1
  5.  
  6. 1 2
  7. 0 2
  8.  
  9. 0 0

Sample Output

  1. Case 1: 2
  2. Case 2: 1

Source

Beijing 2002

题目链接:http://poj.org/problem?id=1328

题意:有一条直的海岸线,上面有雷达。以海岸线为x轴,x轴上面为海,下面为岸。海里面有很多岛屿。已知雷达的观测半径。问最少建多少个雷达能把所有岛屿都覆盖到雷达

的侦测范围内。如果不能覆盖全部的岛屿,按样例输出Case数和-1,反之输出Case数和最少需要建立的雷达数。

分析:首先算出经过每个雷达且平行于x轴的弦:假设雷达覆盖半径为d。则弦的左端点为x-sqrt (d*d-y*y),右端点为x+sqrt(d*d-y*y)。然后按左端点从小到大排序。由于不能受右

端点的影响,将弦的左右端点横坐标用结构体存起来。然后,将第一个雷达放在排序后的弦投影在x轴的右端点temp。从过第2个岛屿的弦的投影开始扫描,如果右端点

<temp即右端点在雷达左边。把雷达移动到此右端点,此时就能使雷达既覆盖到这个岛屿,又覆盖到前面的岛屿。如果左端点在雷达右边,则不能覆盖,需要再建立一个

雷达。然后把新雷达建在此时弦右端点投影到x轴的坐标。忽略前面的,对后面的岛屿进行同样的操作。(贪心思想)

注意:半径和坐标都有可能是小数,所以用double类型比较好,不然用int又要注意计算时*1.000变为浮点数。

代码:

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<cmath>
  6. using namespace std;
  7.  
  8. struct point
  9. {
  10. double left;
  11. double right;
  12. }p[5012];
  13. bool flag;
  14. int cnt;
  15.  
  16. bool cmp(point &x,point &y)
  17. {
  18. return x.left<y.left;
  19. }
  20.  
  21. int main()
  22. {
  23. int cases=0,i,n;
  24. double d;
  25. while(scanf("%d%lf",&n,&d)&&n&&d)
  26. {
  27. flag=true;
  28. double a,b;
  29. for(i=0;i<n;i++)
  30. {
  31. scanf("%lf%lf",&a,&b);
  32. if(fabs(b)>d)
  33. flag=false;
  34. p[i].left=a-sqrt(d*d-b*b);
  35. p[i].right=a+sqrt(d*d-b*b);
  36. }
  37. printf("Case %d: ",++cases);
  38. if(!flag)
  39. puts("-1");
  40. else
  41. {
  42. sort(p,p+n,cmp);
  43. double temp=p[0].right;
  44. cnt=1;
  45. for(i=1;i<n;i++)
  46. {
  47. if(p[i].right<temp)
  48. temp=p[i].right;
  49. else if(p[i].left>temp)
  50. {
  51. cnt++;
  52. temp=p[i].right;
  53. }
  54. }
  55. printf("%d\n",cnt);
  56. }
  57. }
  58. return 0;
  59. }
  60.  
  61. //AC

11920732

fukan

1328

Accepted

188K

32MS

C++

981B

2013-08-05 00:20:15

poj 1328 Radar Installation (简单的贪心)的更多相关文章

  1. 贪心 POJ 1328 Radar Installation

    题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...

  2. POJ 1328 Radar Installation 贪心 A

    POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...

  3. poj 1328 Radar Installation(nyoj 287 Radar):贪心

    点击打开链接 Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43490   Accep ...

  4. poj 1328 Radar Installation(贪心)

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  5. POJ 1328 Radar Installation 【贪心 区间选点】

    解题思路:给出n个岛屿,n个岛屿的坐标分别为(a1,b1),(a2,b2)-----(an,bn),雷达的覆盖半径为r 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的 ...

  6. poj 1328 Radar Installation【贪心区间选点】

    Radar Installation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) ...

  7. POJ 1328 Radar Installation【贪心】

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

  8. poj 1328 Radar Installation(贪心+快排)

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  9. poj 1328 Radar Installation 排序贪心

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 56702   Accepted: 12 ...

随机推荐

  1. memcache 集群

    memcache 是一个分布式的缓存系统,但是本身没有提供集群功能,在大型应用的情况下容易成为瓶颈.但是客户端这个时候可以自由扩展,分两阶段实现.第一阶段:key 要先根据一定的算法映射到一台memc ...

  2. 精致的外观Request

    为什么要说Request精致的外观?请注意,我们这里所说的并不总是理解含义的外观门面,事实上,它使用的立面设计图案.使用的主要考虑数据安全的门面.它涉及到一个更大的系统系统的多个子系统之间的互动沟通. ...

  3. Android应用性能測试

    Android内存限制 java虚拟机有内存使用上限的限制 adb shell进入手机,这此參数被纪录在/system/build.prop中,假设想直接查看能够使用adb shell getprop ...

  4. Matlab强迫症产生的图像

    最近流行的网络迷恋的照片做头像,闲来无事,取matlab获取一个建设者,它可以产生包括0-9以及99+OCD. 原理很easy,图叠加,这里为了降低文件,将数字图片保存在.mat二进制文件里. === ...

  5. 为什么tap事件绑定在document上,而不是对象本身上

    1.在移动端前端开发,click事件有300ms的延时,为了提升用户体验,快速响应.zepto添加了tap事件.tap是在手指触屏横纵向移动距离小于30px,触发tap事件.移动距离的判断是通过tou ...

  6. 读书时间《JavaScript高级程序设计》七:表单

    在HTML中表单是有<form>元素表示,在JS中表单对应的是HTMLFormElement类型. 表单自有的属性和方法: 获取表单 1. 通过ID  document.getElemen ...

  7. BZOJ 2431 HAOI2009 在列的数目的顺序相反 递归

    标题效果:乞讨1~n有都布置在物种的数目相反的顺序k计划数 订购f[i][j]对于前者i原子的反向排列的数j计划数 因此,我们将第一i插入的数1~i-1该装置 能生产0~i-1反向对 再就是 f[i] ...

  8. 学习pthreads,创建和终止多线程

    更CPU多线程编程,通过笔者的研究发现,,pthreads使用日趋广泛.它是螺纹POSIX标准,它定义了一组线程的创建和操作API. 配置环境见上博客文章.配置环境后,只需要加入#include &l ...

  9. java nio 网络框架实现(转)

    maven项目https://github.com/solq360/common 链式编/解码 链路层链式处理 管道管理socket 多协议处理非常方便 仿netty NioEventLoop 单线程 ...

  10. 拆除vs发展c++程序开发过程中产生的.ipch和.sdf文件的方法

    正在使用Visual Studio 2010发展C++当程序,你会发现,有创建一些奇怪的文件.一个叫ipch的目录,和一个与project同名的.sdf文件.并且ipch以下的文件和.sdf文件都非常 ...