Radar

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
描述
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.

输入
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.
样例输入
3 2
1 2
-3 1
2 11 2
0 20 0
样例输出
Case 1: 2
Case 2: 1
题意:给定点集S={(xi,yi)i=1.2.3...n},求用圆心在x轴上,半径为r的圆覆盖S所需的圆的最少个数。
解题思路:先把给定的xi,yi,r转化为x轴上的区间,即圆心所在的区间,这样就转化为了区间选点问题。先对右端点从小到大排序,右端点相同时,左端点从小到大排序。
代码:
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;struct radar
{double a, b;
}r[1005];bool comp(radar a1, radar a2)
{if(a1.b != a2.b) //对右端点从小到大排序return a1.b < a2.b;return a1.a < a2.a; //右端点相同,左端点从小到大排序
}int main()
{int n, d, i, cas = 0;while(~scanf("%d%d",&n,&d) && (n + d)){double x, y;int flag = 0, m = 0;for(i = 0; i < n; i++){scanf("%lf%lf",&x,&y);if(fabs(y) > d){flag = 1; //有覆盖不到的点continue;}double diff = sqrt(d * d - y * y);r[m].a = x - diff;r[m++].b = x + diff;}printf("Case %d: ",++cas);if(flag){printf("-1\n");continue;}sort(r, r + m, comp);int cnt = 1, p = 0;for(i = 1; i < m; i++){if(r[i].a <= r[p].b)continue;else{cnt++;p = i;}}printf("%d\n",cnt);}return 0;
}

NYOJ 287 Radar 贪心之 区间选点相关推荐

  1. 贪心法——区间选点问题

    贪心法--区间选点问题 区间选点问题.数轴上有nn个闭区间[ai,bi][a_i, b_i].取尽量少的点,使得每个区间内都至少有一个点(不同区间内含的点可以是同一个). 贪心思想:先按bb从小到大进 ...

  2. 贪心算法——区间选点问题

    转载:https://blog.csdn.net/xia842655187/article/details/51944763 区间选点的问题大致可以描述为:  给定N个区间[a,b],取尽量少的点,使 ...

  3. 【48. 贪心(区间选点)】

    区间选点与最大不相交区间数量代码一样 思路 将每个区间按照右端点从小到大进行排序 从前往后枚举区间,end值初始化为无穷小 如果本次区间不能覆盖掉上次区间的右端点, ed < range[i]. ...

  4. 贪心算法-区间选点问题-种树

    [题目描述]一条街道的一边有几座房子.因为环保原因居民想要在路边种些树,路边的地区被分割成n块,并被编号为1~n.每块大小为一个单位尺寸且最多可总一棵树.每个居民想在门前种些树并制定了三个数b,e,t ...

  5. 【贪心】AcWing 803. 区间选点

    905. 区间选点 文章目录 题目描述 输入格式: 输出格式: 数据范围 输入样例 输出样例 方法:贪心 解题思路 代码 复杂度分析: 题目描述 给定 N 个闭区间 [ a i , b i ] [a_ ...

  6. [AcWing] 905. 区间选点(C++实现)贪心---区间问题例题

    [AcWing] 905. 区间选点(C++实现)贪心---区间问题例题 1. 题目 2. 读题(需要重点注意的东西) 3. 解法 4. 可能有帮助的前置习题 5. 所用到的数据结构与算法思想 6. ...

  7. 【贪心策略】区间选点问题

    问题描述: Description You are given n closed, integer intervals [ai, bi] and n integers c1,-, cn. Write ...

  8. (贪心)区间问题大致思路

    1.选择不相交区间. a.描述: 数轴上有n个开区间(ai, bi).选择尽量多个区间,使得这些区间两两 没有公共点. b.思路总结: 1.区间x完全包含y,选y 2.按照bi从小到大排序,从第一个区 ...

  9. 区间选点 and 最大不相交区间

    区间选点 题目描述 给定 N 个闭区间 [ai,bi],请你在数轴上选择尽量少的点,使得每个区间内至少包含一个选出的点. 输出选择的点的最小数量. 位于区间端点上的点也算作区间内. 输入输出及样例 最 ...

最新文章

  1. Qt5官方demo分析集29——Extending QML - Property Value Source Example
  2. 【经典回放】多种语言系列数据结构算法:栈(JavaScript版)
  3. the development of c language(转)
  4. iOS客户端流量统计
  5. 面试总是死在网络协议,我该怎么办?
  6. (原創) 用OOP实作矩阵相乘 (C/C++)
  7. 用iframe框架,登录过期后登录框在框架里加载的问题
  8. 织梦搜索结果增加dede:type、dede:arclist、dede:channelartlist、dede:tag等标签支持
  9. 媒体查询(黑马笔记)-移动端布局rem
  10. linux445端口是什么,端口445怎么关 445端口是什么服务 445病毒是什么
  11. docker curl: (56) Recv failure: Connection reset by peer问题解决方法
  12. IPHONE屏幕大小,分辨率解析
  13. 如何提升测试人员在公司的地位
  14. MATLAB实现变限积分函数的积分/ 多重积分/ 如何解决求积分显示AB浮点标量报错
  15. C语言 epoll 工作模式之lt 等级触发 level trigger
  16. 全国各城市经纬度,代码等
  17. 手机蓝牙和蓝牙模块进行通信
  18. win10 安装并配置docker
  19. DCHQ + EMC REX-Ray在多个云和虚拟化平台上交付容器数据库服务
  20. 如何区分安装的oracle数据库是客户端还是服务器端?

热门文章

  1. Harbor管理docker镜像(1.7.5版本-主从复制)
  2. Cisco无线AP上联口为trunk时无法注册故障处理
  3. Ubuntu下搭建NFS,并在开发板挂载
  4. 数学图形(1.5)克莱线
  5. [链接]C++和Python版本的委托
  6. 中国互联网发展的两个瓶颈
  7. why wechat is not a good place for the learning, but csdn is
  8. 原来在UNITY中使用system.io下的所有函数都可以用相对路径 : Assets/xx
  9. C语言增量内存申请 realloc
  10. 【转】Unity3D将来时:IL2CPP(上)