POJ1328Radar Installation(区间点覆盖问题)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 68597 | Accepted: 15373 |
Description
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
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
test case number followed by the minimal number of radar installations needed.
"-1" installation means no solution for that case.
Sample Input
3 2
1 2
-3 1
2 1 1 2
0 2 0 0
Sample Output
Case 1: 2
Case 2: 1 http://blog.csdn.net/zxiaopp/article/details/48845201
数轴上有n个闭区间[ai,bi]。取尽量少的点,使得每个区间内都至少有一个点(不同区间内含的点可以是同一个)。
贪心策略:
按照b1<=b2<=b3…(b相同时按a从大到小)的方式排序排序,从前向后遍历,当遇到没有加入集合的区间时,选取这个区间的右端点b。
证明:
为了方便起见,如果区间i内已经有一个点被取到,我们称区间i被满足。
1、首先考虑区间包含的情况,当小区间被满足时大区间一定被满足。所以我们应当优先选取小区间中的点,从而使大区间不用考虑。
按照上面的方式排序后,如果出现区间包含的情况,小区间一定在大区间前面。所以此情况下我们会优先选择小区间。
则此情况下,贪心策略是正确的。
不一定要按照正规解法,第一次我b相同,并不是按照a从大到小排序,而是a从小到大排序,其实怎么排序都可以,只要看最后的截止时间就ok了,如果下一个的开始时间在截止时间之前,那么可以用一个点覆盖,如果在后面,只需要两个点,然后更新到后面那个的截止时间,只要开始时间在截止前面一定不需要新的一个点来覆盖了。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
const int MAX = + ;
const double eps = 10e-;
struct Rader
{
double a,b;
};
Rader rader[MAX];
int cmp(Rader x, Rader y)
{
if(fabs(x.b - y.b) <= eps)
return x.a < y.a;
return (x.b - y.b) < -eps;
}
int main()
{
int n,d,flag,tase = ;
double x, y, t;
while(scanf("%d%d", &n, &d) != EOF)
{
if(n == && d == )
break;
flag = ;
for(int i = ; i <= n; i++)
{
scanf("%lf%lf", &x, &y);
if(d < y)
{
flag = ;
continue;
}
t = sqrt(d * d - y * y);
rader[i].a = x - t;
rader[i].b = x + t;
}
printf("Case %d: ",++tase);
if(flag)
{
printf("-1\n");
}
else
{
sort(rader + , rader + + n, cmp);
int cnt = ;
double start = rader[].b;
for(int i = ; i <= n; i++)
{
if(start - rader[i].a < -eps)
{
start = rader[i].b;
cnt++;
}
}
printf("%d\n", cnt);
} } return ;
}
b从小到大排序
之后又想了想可不可以针对a来排序,结果也是可以的,就对a从小到大咯,如果下一个的截止时间在这个截止时间之前不用新的点覆盖,但要更新到后一个的截止时间,对,因为要以小区间为基准;如果下一个的开始时间在截止时间之后那么一定得需要一个新的点来覆盖了,同时更新到后一个的截止时间
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
const int MAX = + ;
const double eps = 10e-;
struct Rader
{
double a,b;
};
Rader rader[MAX];
int cmp(Rader x, Rader y)
{
// if(fabs(x.b - y.b) <= eps)
// return x.a < y.a;
return (x.a - y.a) < -eps;
}
int main()
{
int n,d,flag,tase = ;
double x, y, t;
while(scanf("%d%d", &n, &d) != EOF)
{
if(n == && d == )
break;
flag = ;
for(int i = ; i <= n; i++)
{
scanf("%lf%lf", &x, &y);
if(d < y)
{
flag = ;
continue;
}
t = sqrt(d * d - y * y);
rader[i].a = x - t;
rader[i].b = x + t;
}
printf("Case %d: ",++tase);
if(flag)
{
printf("-1\n");
}
else
{
sort(rader + , rader + + n, cmp);
int cnt = ;
double start = rader[].b;
for(int i = ; i <= n; i++)
{
if(start - rader[i].b > eps)
{
start = rader[i].b;
}
else if(start - rader[i].a < -eps)
{
start = rader[i].b;
cnt++;
}
}
printf("%d\n", cnt);
} } return ;
}
a从小到大
POJ1328Radar Installation(区间点覆盖问题)的更多相关文章
- UVAlive 2519 Radar Installation (区间选点问题)
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...
- poj1328Radar Installation 贪心
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 64472 Accepted: 14 ...
- POJ1328Radar Installation
http://poj.org/problem?id=1328 题的大意就是说在海里有小岛,坐标位置会给出,需要岸边的雷达覆盖所有的小岛,但雷达的覆盖范围有限,所以,需要最少的雷达覆盖所有的小岛,但若是 ...
- POJ1328Radar Installation(贪心)
对于每一个点,可以找到他在x轴上的可行区域,这样的话就变为了对区间的贪心. #include<iostream> #include<stdio.h> #include<s ...
- Poj1328Radar Installation雷达安装
原题链接 经典贪心,转化为问题为,对于所有的区间,求最小的点数能使每个区间都至少有一个点. #include<iostream> #include<cstdio> #inclu ...
- 【贪心】POJ1328-Radar Installation
[思路] 以每一座岛屿为圆心,雷达范围为半径作圆,记录下与x轴的左右交点.如果与x轴没交点,则直接退出输出“-1”.以左交点为关键字进行排序,从左到右进行贪心.容易知道,离每一个雷达最远的那一座岛与雷 ...
- poj1328 Radar Installation 区间贪心
题目大意: 在X轴选择尽量少的点作为圆心,作半径为d的圆.使得这些圆能覆盖所有的点. 思路: 把每个点都转化到X轴上.也就是可以覆盖这个点的圆心的位置的范围[a,b].然后按照每个点对应的a从小到大排 ...
- UVA 11134 FabledRooks 传说中的车 (问题分解)
摘要:贪心,问题分解. 因为行列无关,所以这个二维问题可以分解成两个一维问题. 优先队列实现:类似区间点覆盖的问题,先按照左端点排序,相同然后在按右端点排序(灵活性小的优先选).最优的选法,当然是要使 ...
- poj 1328 Radar Installation【贪心区间选点】
Radar Installation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) ...
随机推荐
- CSS3弹性伸缩布局(二)——flex布局
上一篇博客<CSS3弹性伸缩布局(一)——box布局>介绍了旧版本的box布局,而这篇博客将主要介绍最新版本的flex布局的基础知识. 新版本简介 新版本的Flexbox模型是2012年9 ...
- iOS数据本地持久化
p1:归档.Preference(NSUserDefault).沙盒存储 iOS开发中本地存储主要有三种形式 XML属性列表(plist)归档 Preference(偏好设置) NSKeyedAr ...
- Android应用性能优化之使用SparseArray替代HashMap
HashMap是java里比较常用的一个集合类,我比较习惯用来缓存一些处理后的结果.最近在做一个Android项目,在代码中定义这样一个变量,实例化时,Eclipse却给出了一个 performanc ...
- mvc control 请求两次问题
今天在做项目时,突然发现一个mvc 的control中action被执行了两次,最终发现是由于favicon.ico导致的.问题代码: <link rel="shortcut icon ...
- 模拟alert和confirm
啥也不说,先上图,有图有真相 :) 现在绝大多数网站都不用自带的alert和confirm了,因为界面太生硬了.因此这个插件就这样产生了... 来看插件的实现代码吧: (function () { $ ...
- CSS 实现加载动画之四-圆点旋转
圆点旋转也是加载动画中经常用到的.其实现方式和菊花旋转一样,只不过一个是线条形式,一个是圆点形式.圆点按照固定的旋转角度排列,加上延时的改变透明度的动画就可以实现.这个实现也比较简单. 1. 动画截图 ...
- 网络请求怎么样和UI线程交互? Activity2怎么通知Activity1 更新数据
1.网络请求怎么样和UI线程交互? 目前我的做法是,建立线程池管理网络请求线程,通过添加task来新增网络请求.所有的网络操作通过统一的request来实现,网络返回结果通过回调onError和onS ...
- [Android] emualtor-5554 offline的解决方法
现象:用adb devices命令总发现emualtor-5554 offline,在.android目录下面并没有发现这个设备,没法删除.原因:有程序占用5555端口,导致adb认为5554不能作为 ...
- Linux(9.14-9.20)学习笔记
实验一 Linux系统简介 一.Linux 为何物 Linux 就是一个操作系统,Linux 也就是系统调用和内核那两层. 二.Linux 历史简介 操作系统始于二十世纪 50 年代,当时的操作系统能 ...
- Opencv Linux环境搭建(2)
继上次ubuntu10.04搭建失败之后,这次又换了一个系统. 拿出之前闲置的笔记本,安装了ubuntu12.04,按照这里的教程开始搞起来: http://www.linuxidc.com/Linu ...