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 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的 ...
随机推荐
- 类似QQ在线离线好友界面
把头像设置成圆形的代码如下: package com.example.lesson6_11_id19; import android.content.Context; import android.c ...
- linux php扩展安装gettext
php解压后的文件路径为/usr/local/src/php-5.2.6 php 的安装路径为/usr/local/php [root@localhost# cd /usr/local/src/ph ...
- Linux服务器用iotop命令分析服务器磁盘IO情况
Linux下的IO统计工具如iostat, nmon等大多数是只能统计到per设备的读写情况, 如果你想知道每个进程是如何使用IO的就比较麻烦.如果会systemtap, 或者blktrace这些事情 ...
- Javaweb学习笔记4—Reuest&Response
今天来讲javaweb的第四段学习. Request和Response还是比较重要的 老规矩,首先先用一张思维导图来展现今天的博客内容. ps:我的思维是用的xMind画的,如果你对我的思维导图感兴趣 ...
- attr和prop的区别
由于prop(property的缩写)和attr(attribute的缩写)翻译成汉语,均有“特性.属性”等意思的原因,导致大家容易混淆分不清. (1)在处理自定义时属性时,用attr(),若用pro ...
- WebDriverException: Message: unknown error: Chrome failed to start: crashed
the last answer WebDriverException: Message: unknown error: Chrome failed to start: crashed
- UVA12633 Super Rooks on Chessboard
题目描述 题解: 第一眼满眼骚操作,然后全部否掉. 然后屈服于题解,才发现这题这么执掌. 首先,如果这个东西是普通的车,那我们可以记录一下$x,y$的覆盖情况,然后减一下; 但是这个可以斜着走. 所以 ...
- <Spring Cloud>入门二 Eureka Client
1.搭建一个通用工程 1.1 pom 文件 <?xml version="1.0" encoding="UTF-8"?> <project x ...
- LINUX:关于Redis集群的节点分配
文章来源:http://www.cnblogs.com/hello-tl/p/7808268.html 根据上述 Redis集群搭建:http://www.cnblogs.com/hello-tl/ ...
- LeetCode(10) Regular Expression Matching
题目 Implement regular expression matching with support for '.' and '*'. '.' Matches any single charac ...