Greedy:Radar Installation(POJ 1328)

题目大意,就是令在海岸线的(直线)一边是海(y>0),另一边是陆地(y<=0),在海岸线上装雷达,雷达可以覆盖的范围为d,海上有岛,(x,y),问你应该怎么装雷达,才能做到技能雷达的检测范围覆盖全部的岛,又能使雷达安装最少?(不能做到输出-1)
我的思路,首先是把岛排一次序,然后从x坐标最小的那个岛开始贪心算法,怎么做呢?那就是用x=sqrt(d*d-y*y)+xa,来获得当前岛的安装最远的雷达的位置,然后不断往前找,在超越当前雷达圆心距离之前,不断缩小雷达的位置,使雷达能覆盖到更多的岛,直到岛的横坐标超出雷达中心。
接下来把当前的雷达中心为圆心,把覆盖在圆内的岛全部找出来,这能使一个雷达能覆盖岛最多,然后从最靠近雷达的第一个没被覆盖的岛开始,重复上述过程。
#include <iostream>
#include <functional>
#include <algorithm>
#include <math.h> using namespace std;
typedef double Position;
typedef struct coordinate
{
Position x;
Position y;
}Pos;
int fcmop(const void* a, const void *b)
{
return (*(Pos *)a).x > (*(Pos *)b).x ? : -;
} static Pos islands[];
static bool used[];
int Search(const int, const int);
bool Judge(const int,const int);
double dist(Pos, Position);
Position get_radar_pos(Pos, const int); int main(void)
{
int Sum_Of_Islands, Distance_Of_Radar, k = , ans;
while (~scanf("%d%d", &Sum_Of_Islands, &Distance_Of_Radar))
{
if (Sum_Of_Islands == && Distance_Of_Radar == )
break;
for (int i = ; i < Sum_Of_Islands; i++)
scanf("%lf%lf", &islands[i].x, &islands[i].y);
qsort(islands, Sum_Of_Islands, sizeof(Pos), fcmop);//把岛都按照顺序排序
memset(used, , sizeof(used));
ans = Search(Sum_Of_Islands, Distance_Of_Radar); printf("Case %d: %d\n", k++, ans);
} return ;
} double dist(Pos a, Position mid)//计算两点之间的距离
{
return sqrt((a.x - mid)*(a.x - mid) + a.y*a.y);
} Position get_radar_pos(Pos a, const int dist)
{
Position k = sqrt((double)(dist*dist) - a.y*a.y) + a.x;
return k;
} bool Judge(const int j, const int Distance_Of_Radar)
{
if (islands[j].y - Distance_Of_Radar > )//不可能找到一个雷达覆盖这个岛,直接退出
return ;
else if (islands[j].y < )
return ;
else return ;
} int Search(const int Sum_Of_Islands, const int Distance_Of_Radar)
{
Position tmp_pos, now_min_pos;
int ans = , tmp_mid, j, now_is = ; if (Distance_Of_Radar <= )//Distance小于0,直接再见
return -;
for (; now_is < Sum_Of_Islands;)
{
if (Judge(now_is, Distance_Of_Radar)) return -;
tmp_mid = now_is;
now_min_pos = get_radar_pos(islands[now_is], Distance_Of_Radar);//初始化最远位置
used[now_is] = ;
for (j = now_is + ; j < Sum_Of_Islands && islands[j].x <= now_min_pos; j++)
{
if (used[j]) continue;//必须没被标记过
if (Judge(j, Distance_Of_Radar)) return -;
used[j] = ;//记得标记
tmp_pos = get_radar_pos(islands[j], Distance_Of_Radar);
if (tmp_pos <= now_min_pos)//必须是不能被覆盖才更新
{
now_min_pos = tmp_pos;
tmp_mid = j;//更新是哪一个岛才是中间岛
}
}
ans++;//建立雷达点
for (j = tmp_mid; j < Sum_Of_Islands && islands[j].x <= now_min_pos + Distance_Of_Radar; j++)//把圈内的岛都排除
{
if (Judge(j, Distance_Of_Radar)) return -;
else if (dist(islands[j], now_min_pos) <= Distance_Of_Radar)
used[j] = ;
}
for (j = tmp_mid; j < Sum_Of_Islands && used[j]; j++);//在中心往前找找到第一个没有被标记的点
now_is = j;
}
return ans;
}

代码的变量命名已经能很清楚说明我在干什么了
另外其实代码还可以简化一下,那就是去除used域,其实我们在上面找在雷达圆心右方到那些没被覆盖的岛的时候,我们忽略了其实新的圆也可以与当前作用域重复的,那么我们可以就把used区域去掉,然后把代码改成只要找到在半径外,我们就退出循环。(我就不改了)
另外这一道题其实是挺著名的区间贪婪算法,只是我没有用区间去做而已
要做也很简答,思路就是以岛为圆心,雷达监控范围为半径,画圆,与x轴会有两个交点,然后不断去掉重复区间就可以了,如果有区间是重复的,我们就不断缩小区间,如果区间没有交集了,我们就添加一个雷达,重复步骤
详情见:http://www.cnblogs.com/yu-chao/archive/2011/07/18/2109756.html
另种方法速度差不多,都是O(n)
最近真的是忙死我了,都没什么时间做题,哎
Greedy:Radar Installation(POJ 1328)的更多相关文章
- Radar Installation POJ - 1328
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...
- Radar Installation POJ - 1328(贪心)
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...
- Radar Installation POJ - 1328 (贪心)
题目大意(vj上的翻译版本) 假定海岸线是无限长的直线.陆地位于海岸线的一侧,海洋位于另一侧.每个小岛是位于海洋中的一个点.对于任何一个雷达的安装 (均位于海岸线上),只能覆盖 d 距离,因此海洋中的 ...
- 贪心 POJ 1328 Radar Installation
题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...
- Poj 1328 / OpenJudge 1328 Radar Installation
1.Link: http://poj.org/problem?id=1328 http://bailian.openjudge.cn/practice/1328/ 2.Content: Radar I ...
- poj 1328 Radar Installation (简单的贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42925 Accepted: 94 ...
- 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(nyoj 287 Radar):贪心
点击打开链接 Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43490 Accep ...
- poj 1328 Radar Installation【贪心区间选点】
Radar Installation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) ...
随机推荐
- 从Yii2的Request看其CSRF防范策略
用ajax请求还是用命令行CURL请求总是会得到 http400:Bad Request的错误, 而如果用Web网页方式GET访问(去除verbFilter的POST限制),是正常的,是CSRF验证的 ...
- MySQL tips (日期时间操作/concat 等)
1. Query结尾要加一个分号: 2. 数据库和表 SHOW DATABASES; USE YOUR_DB; SHOW TABLES; SHOW COLUMNS FROM study或者D ...
- make xconfig时,出现“Unable to find any QT installation"错误
在make menuconfig的时候,提示是找不到libncurses,最后其实是找不到libncurses-dev,又因为debian系统装的是libncurses5,所以装了libncurses ...
- mq安装参考
CentOS 6.2 64bit 安装erlang及RabbitMQ Server 1.操作系统环境(CentOS 6.2 64bit) [root@leekwen ~]# cat /etc/issu ...
- pthread多线程编程的学习小结
pthread多线程编程的学习小结 pthread 同步3种方法: 1 mutex 2 条件变量 3 读写锁:支持多个线程同时读,或者一个线程写 程序员必上的开发者服务平台 —— DevSt ...
- Lua函数之二
Lua函数之二 Lua中函数的两个重要特性: 1.函数和其他类型(如number.string)一样,可以存放在变量中,也可以存放在table中,可以作为函数的参数,还可以作为函数的返回值. 2.嵌套 ...
- cmd+lcx+nc+sc提权工具总结
cmd:执行命令的载体cmdshell lcx:端口映射工具 1.在自己的host上的cmd下运行:lcx.exe -listen 51 3389 //意思是监听51端口并转发到3389端口 2.在服 ...
- Jquery中的 height(), innerHeight() outerHeight()区别
jQuery中的 height innerHeight outerHeight区别 标准浏览器下: height:高度 innerHeight:高度+补白 outerHeight:高度+补白+边框,参 ...
- C#编程总结 dynamic(转)
介绍 Visual C# 2010 引入了一个新类型 dynamic. 该类型是一种静态类型,但类型为 dynamic 的对象会跳过静态类型检查. 大多数情况下,该对象就像具有类型 object 一样 ...
- curl模拟post请求
1,curl -d "userType=seller&userId=1034285" "www.baidu.com/getInfo.php" curl ...