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 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的 ...
随机推荐
- 杨辉三角python的最佳实现方式,牛的不能再牛了
def triangles(): N = [1] while True: yield N N.append(0) N = [N[i-1] + N[i] for i in range(len(N))] ...
- AI学习一:环境安装
对于Python开发用户来讲,PIP安装软件包是家常便饭.但国外的源下载速度实在太慢,浪费时间.而且经常出现下载后安装出错问题.所以把PIP安装源替换成国内镜像,可以大幅提升下载速度,还可以提高安装成 ...
- QProcess执行带管道的shell命令
QStringList options; options << "-c" << "ls -l | grep a | sort"; QPr ...
- 四次元新浪微博客户端Android源码
四次元新浪微博客户端Android源码 源码下载:http://code.662p.com/list/11_1.html [/td][td] [/td][td] [/td][td] 详细说明:http ...
- 一个PHP开发APP接口的视频教程
感觉php做接口方面的教程很少,无意中搜到了这个视频教程,希望能给一些人带来帮助http://www.imooc.com/learn/163
- java工作流activiti的步骤
链接:activiti 表名称的解释 工作流从流程定义到创建一个流程实例完成执行步骤(省略bpmn的画法) 工作流的所有操作都是使用流程引擎来进行操作的,流程引擎只是存储流程的过程,而不存储具体的业务 ...
- 7-Java-C(小题答案)
1:58497 2:171700 3:145 4:i + j+2 == k+1 || i + k+2 == j+1 || k + j+2 == i+1 5:s + " " + (c ...
- PHP11 日期和时间
学习要点 UNIX时间戳 将其他格式的日期转成UNIX时间戳格式 基于UNIX时间戳的日期计算 获取并格式化输出日期 修改PHP的默认时间 微秒的使用 Unix时间戳 相关概念 Unix tim ...
- jquery 定位
jquery 定位 <html> <head> <title>jquery 定位</title> </head> <body> ...
- spring注解开发-声明式事务(源码)
1. 环境搭建与测试 1)导入相关依赖 数据源.数据库驱动.Spring-jdbc模块 <dependency> <groupId>org.springframework< ...