poj 1328 安雷达问题 贪心算法
题意:雷达如何放置?在xoy二维平面坐标系里面,x轴上方的为岛屿,x轴下方的是雷达要放到位置,如何放使得雷达放的最少?
思路
- 肯定放在x轴上减少浪费是最好的选择
- 什么情况下,雷达无法到达呢?--以这个岛屿为圆心,d为半径,如果这个圆与x轴没有交点的话,就是无法覆盖到---即y>d
- 现在就可以这么想了,每个岛屿都当成圆心C,然后都取画圆,跟x轴的交点例如交点A,B.如果雷达放到AB之间那就是可以将刚才C覆盖住了。--转为区间贪心问题
区间贪心问题:
- 先将各个区间的左端点按照从小到大排序
- 然后按递增序列开始选取区间,有两种情况 实例说明
pos---为当前位置 lefe,right分别为当前区间的左右端点
if(pos<left){ num++;pos=right;}
if(pos>right) {pos=right;}
注:针对本题num为雷达的数量
数学方法
C(x,y) 圆的方程(X-x)^2+(Y-y)^2=d^2 与x轴的交点为 x+根号(d^2-y^2) ,x-根号(d^2-y^2)
解题代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
using namespace std;
typedef struct Node
{
double left, right;
}node;
int cmp(node a, node b)
{
return a.left < b.left;
}
int x[], y[];
node dis[];
int main()
{
int n, d;
double z;
int ans = ;
while (scanf("%d%d", &n, &d) != EOF)
{
if (n == && d == )
break;
else
{
int flag = ;
ans++;
for (int i = ; i < n; i++)
{
scanf("%d%d", &x[i], &y[i]);
if (y[i] > d)
flag = ;
else
{
z = sqrt(d*d*1.0 - y[i] * y[i] * 1.0);
dis[i].left = x[i] - z;
dis[i].right = x[i] + z;
}
}
if (!flag)
{
printf("Case %d: -1\n", ans);
}
else
{
sort(dis, dis + n, cmp);
int num = ;
double p = -;
for (int i = ; i < n; i++)
{
if (dis[i].left > p)
{
num++;
p = dis[i].right;
}
else if (dis[i].right < p)
{
p = dis[i].right;
}
}
printf("Case %d: %d\n", ans, num);
}
}
}
return ;
}
poj 1328 安雷达问题 贪心算法的更多相关文章
- POJ 1328 Radar Installation【贪心】
POJ 1328 题意: 将一条海岸线看成X轴,X轴上面是大海,海上有若干岛屿,给出雷达的覆盖半径和岛屿的位置,要求在海岸线上建雷达,在雷达能够覆盖全部岛屿情况下,求雷达的最少使用量. 分析: 贪心法 ...
- POJ 1328 安装雷达 (贪心)
<题目链接> 题目大意: 以x轴为分界,y>0部分为海,y<0部分为陆地,给出一些岛屿坐标(在海中),再给出雷达可达到范围,雷达只可以安在陆地上,问最少多少雷达可以覆盖所以岛屿 ...
- poj 1328 Radar Installatio【贪心】
题目地址:http://poj.org/problem?id=1328 Sample Input 3 2 1 2 -3 1 2 1 1 2 0 2 0 0 Sample Output Case 1: ...
- POJ 1328 Radar Installation【贪心 区间问题】
题目链接: http://poj.org/problem?id=1328 题意: 在x轴上有若干雷达,可以覆盖距离d以内的岛屿. 给定岛屿坐标,问至少需要多少个雷达才能将岛屿全部包含. 分析: 对于每 ...
- 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 【贪心 区间选点】
解题思路:给出n个岛屿,n个岛屿的坐标分别为(a1,b1),(a2,b2)-----(an,bn),雷达的覆盖半径为r 求所有的岛屿都被覆盖所需要的最少的雷达数目. 首先将岛屿坐标进行处理,因为雷达的 ...
- poj 1328 Radar Installation【贪心区间选点】
Radar Installation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) ...
- poj 2586 Y2K Accounting Bug(贪心算法,水题一枚)
#include <iostream> using namespace std; /*248K 32MS*/ int main() { int s,d; while(cin>> ...
- poj 1328 Radar Installation 排序贪心
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 56702 Accepted: 12 ...
随机推荐
- Could not load file or assembly 'Oracle.ManagedDataAccessDTC.DLL' or one of its dependencies.
Could not load file or assembly 'Oracle.ManagedDataAccessDTC.DLL' or one of its dependencies. 不是有效的 ...
- ListView 视图(View)
自定义视图,设置默认ListView,ListViewItems默认样式 public class VirtualStackPanelView : ViewBase { public static r ...
- Linq 根据list属性去重复
s.Where((x, i) => s.FindIndex(z => z.ArticleTitle == x.ArticleTitle) == i).ToList();
- SQL函数TIMEDIFF在Java程序中使用报错的问题分析
需求背景 (读者可略过)司机每天从早到晚都会去到不同的自动售货机上补货,而且补货次数和路线等也是因人而异,补货依据是由系统优化并指派.但是目前系统还无法实施有效指挥和优良的补货策略,司机的补货活动因此 ...
- Android Doze模式源码分析
科技的仿生学无处不在,给予我们启发.为了延长电池是使用寿命,google从蛇的冬眠中得到体会,那就是在某种情况下也让手机进入类冬眠的情况,从而引入了今天的主题,Doze模式,Doze中文是打盹儿,打盹 ...
- getuser
Help on function getuser in module getpass: getuser() Get the username from the environment or pa ...
- Bezier(贝塞尔曲线)
CDC::PolyBezierBOOL PolyBezier( const POINT* lpPoints, int nCount ); 和 曲线原理及多段曲线连接处如何光滑连接:第一段曲线要有4个点 ...
- 在SAP C4C里触发SAP ERP的ATP check和Credit check
在C4C里创建一个新的Sales Quote: 添加三个行项目: 执行action "Request External Pricing"会从ERP更新pricing信息,触发ATP ...
- UVA 1153 Keep the Customer Satisfied 顾客是上帝(贪心)
因为每增加一个订单,时间是会增加的,所以先按截止时间d排序, 这样的话无论是删除一个订单,或者增加订单,都不会影响已经选好的订单. 然后维护一个已经选好的订单的大根堆(优先队列),如果当前无法选择的话 ...
- UVA 1613 K-Graph Oddity K度图着色 (构造)
题意:在一个n个点的无向连通图中,n是奇数,k是使得所有点的度数不超过k的最小奇数,询问一种染色方案,使得相邻点的颜色不同. 题解:一个点和周围的点的颜色数加起来最大为它的度数+1:如果最大度数是偶数 ...