POJ 1328 Radar Installation【贪心 区间问题】
题目链接:
http://poj.org/problem?id=1328
题意:
在x轴上有若干雷达,可以覆盖距离d以内的岛屿。
给定岛屿坐标,问至少需要多少个雷达才能将岛屿全部包含。
分析:
对于每个岛屿,计算出可以包含他的雷达所在的区间,找到能包含最多岛屿的区间即可。
可以看出这是一个典型的区间问题,我们有几种备选方法:
(1)优先选取右端点最大的区间。
(2)优先选取长度最长的区间。
(3)优先选取与其他区间重叠最少的区间。
2.3很容易想到反例,而1则是正确的,端点越靠右,剩下的区间就越少,需要的雷达就越少。
代码:
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 1005, INF = 0x3f3f3f3f;
struct node
{
double x, y;
bool operator <(const node &a)const{
return y > a.y;
}
};
node p[maxn];
int main (void)
{
int n, d;
int Case = 1;
while(scanf("%d%d", &n, &d) && (n != 0 ||d != 0)){
int x, y;
int maxy = -INF;
for(int i = 0; i < n; i++) {
scanf("%d%d", &x, &y);
maxy = max(maxy, y);
double dis = sqrt(1.0 * d * d - 1.0 * y * y);
p[i].x = 1.0 * x - dis;
p[i].y = 1.0 * x + dis;
}
if(maxy > d || d < 0) {
printf("Case %d: -1\n", Case++);
continue;
}
sort(p, p + n);
int cnt = 1;
double t = -INF;
for(int i = 0; i < n; i++){
if(t > p[i].y ) {
cnt++;
t = p[i].x;
}
else t = max(p[i].x, t);
}
printf("Case %d: %d\n", Case++, cnt);
}
}
POJ 1328 Radar Installation【贪心 区间问题】的更多相关文章
- POJ - 1328 Radar Installation(贪心区间选点+小学平面几何)
Input The input consists of several test cases. The first line of each case contains two integers n ...
- POJ 1328 Radar Installation 贪心 A
POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...
- poj 1328 Radar Installation(贪心+快排)
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- POJ 1328 Radar Installation 贪心算法
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- poj 1328 Radar Installation(贪心)
题目:http://poj.org/problem?id=1328 题意:建立一个平面坐标,x轴上方是海洋,x轴下方是陆地.在海上有n个小岛,每个小岛看做一个点.然后在x轴上有雷达,雷达能覆盖的范 ...
- POJ 1328 Radar Installation 贪心 难度:1
http://poj.org/problem?id=1328 思路: 1.肯定y大于d的情况下答案为-1,其他时候必定有非负整数解 2.x,y同时考虑是较为麻烦的,想办法消掉y,用d^2-y^2获得圆 ...
- POJ 1328 Radar Installation 贪心题解
本题是贪心法题解.只是须要自己观察出规律.这就不easy了,非常easy出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 依照x轴大小排序 2 从最左边的点循环.首先找到最小x轴的圆 ...
- POJ 1328 Radar Installation#贪心(坐标几何题)
(- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<algorithm> #include<cmath ...
- 贪心 POJ 1328 Radar Installation
题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...
- POJ 1328 Radar Installation 【贪心 区间选点】
解题思路:给出n个岛屿,n个岛屿的坐标分别为(a1,b1),(a2,b2)-----(an,bn),雷达的覆盖半径为r 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的 ...
随机推荐
- Android开发实现高德地图定位
1.获取Key 参考官方文档:http://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key 对于签名文件的获取建议 ...
- centos开机直接进入命令行
找到文件,/etc/inittab 文件,在下面有一行 id:5:initdefault: 将上面的5改成3就可以了 5是图形界面 3是命令行界面 就是文本界面.
- Elasticsearch插件清单
1.API插件:主要对Elasticsearch添加的API特性或者功能,通常用于搜索或者映射 2. 报警插件: 当Elasticsearch的索引指标超过阀值时就会触发 3. 分词插件:ik是比较好 ...
- x86和i386
x86: 1 9 7 8年6月,I n t e l公司推出了8 0 8 6,一个1 6位微处理器,它可访问的存储空间达到1 M B. Intel x86家族继续发展,1 9 8 5年出现了3 2位的3 ...
- Matlab 图像转极坐标系
实心圆环 imgVP1=flip(imgVP1,1); % 水平翻转 polarVP1=polarVolinPlot(imgVP1); % 调用函数空心圆环 [m,n,~]=size(imgVP2) ...
- contos7 使用zabbix监控物理磁盘状态实例
一.系统环境: 物理机:dell R640 操作系统:centos7 二.安装MegaCli 监控主要是通过MegaCli 软件获取到物理主机的read及硬盘相关状态信息.然后通过zabbix的自定义 ...
- jeecms
===标签=== <!-- 显示一级栏目对应的二级栏目 --> <!-- [@cms_channel_list parentId=c.id] [#if tag_list?size&g ...
- vscode 用户代码片段 vue初始化模板 Snippet #新加入开头注释 自动生成文件名 开发日期时间等内容
vue文件模板 模板变量 https://code.visualstudio.com/docs/editor/userdefinedsnippets#_variables vue.json { // ...
- integer to roman leetcode c++实现
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- java程序中中文没有乱码,存入数据库后中文乱码问题
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/sys_user?useOldAliasMetadataBe ...