贪心 POJ 1328 Radar Installation
题目地址:http://poj.org/problem?id=1328
/*
贪心
(转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿,
这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知道,
问最少需要多少个雷达覆盖所有的岛屿。
(错误)思路:我开始是想从最左边的点雷达能探测的到的最右的位置出发,判断右边其余的点是否与该点距离小于d
是,岛屿数-1;不是,雷达数+1,继续。。。
(正确)思路:每个岛屿的座标已知,以雷达半径为半径画圆,与x轴有两个交点。
也就是说,若要覆盖该岛,雷达的位置范围是这两个交点。因此转化为覆盖区间的问题。
参考代码:http://www.cnblogs.com/kuangbin/archive/2011/07/30/2121838.html
*/
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
using namespace std; const int MAXN = 1e3 + ;
const int INF = 0x3f3f3f3f;
struct NODE
{
int x, y;
double l, r;
}node[MAXN]; bool cmp(NODE a, NODE b)
{
return a.l < b.l;
} bool ok(int n, int d)
{
if (d < ) return false;
for (int i=; i<=n; ++i)
{
if (node[i].y > d) return false;
} return true;
} void work(int n, int d)
{
int cnt = ;
double now = node[].r;
for (int i=; i<=n; ++i)
{
if (now > node[i].r) now = node[i].r;
if (now < node[i].l)
{
now = node[i].r;
cnt++;
}
}
printf ("%d\n", cnt);
} int main(void) //POJ 1328 Radar Installation
{
//freopen ("I.in", "r", stdin); int n, d;
int cnt = ;
while (~scanf ("%d%d", &n, &d) && n && d)
{
for (int i=; i<=n; ++i)
{
scanf ("%d%d", &node[i].x, &node[i].y);
node[i].l = (double)node[i].x - sqrt ((double)d * d - node[i].y * node[i].y);
node[i].r = (double)node[i].x + sqrt ((double)d * d - node[i].y * node[i].y);
}
sort (node+, node++n, cmp);
printf ("Case %d: ", ++cnt);
if (!ok (n, d))
{
printf ("%d\n", -); continue;
}
work (n, d);
} return ;
} /*
void work(int n, int d)
{
int i = 1;
int j = 1;
int next = 1;
int num = 0;
int cnt = 0;
while (num < n)
{
double res = node[i].x + sqrt (d * d - node[i].y * node[i].y);
cnt++; num++;
int flag = 0;
fors (j=i+1; j<=n; ++j)
{
if (pow (node[j].x - res, 2) + pow (node[j].y, 2) <= d * d)
{
num++; next = j; flag = 1;
}
}
if (flag) i = next + 1;
else i++;
}
printf ("%d\n", cnt);
}
*/
贪心 POJ 1328 Radar Installation的更多相关文章
- 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 (简单的贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42925 Accepted: 94 ...
- 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(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) ...
- POJ 1328 Radar Installation【贪心】
POJ 1328 题意: 将一条海岸线看成X轴,X轴上面是大海,海上有若干岛屿,给出雷达的覆盖半径和岛屿的位置,要求在海岸线上建雷达,在雷达能够覆盖全部岛屿情况下,求雷达的最少使用量. 分析: 贪心法 ...
- 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 排序贪心
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 56702 Accepted: 12 ...
随机推荐
- 360双击ctrl搜索可能会与firefox快捷键冲突
最近使用火狐浏览器时有好几次要在网页上的对话框输入文字时出现问题,按下字母键直接跳出了firefox菜单选项,用鼠标重新定位到输入位置再打还是不行,照样会弹出菜单提示,如下图,这可能是有什么快捷键冲突 ...
- LVS负载均衡集群服务搭建详解(二)
lvs-nat模型构建 1.lvs-nat模型示意图 本次构建的lvs-nat模型的示意图如下,其中所有的服务器和测试客户端均使用VMware虚拟机模拟,所使用的CentOS 7 VS内核都支持ipv ...
- IOS 页面之间的跳转
1.UINavigationController popToViewController 对应popViewControllerAnimated: 也可以使用: [self.navigationCon ...
- poj3026(bfs+prim)
The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. ...
- 解决php configure: error: Cannot find ldap libraries in /usr/lib.错误
解决php configure: error: Cannot find ldap libraries in /usr/lib.错误 iitshare 分类:Linux,PHP,项目实施 | 标签:Ca ...
- ecshop的商品系统数据库整合
-- 表的结构 `ecs_shop_config` '全站配置信息表' 商店设置 `id` '全站配置信息自增id',`parent_id` '父节点id,取值于该表id字段的值',`co ...
- (转)maven eclipse debug
准备工作: a. 在pom.xml中配置jetty插件: <plugins> <plugin> <groupId>org.mortbay.jetty</gro ...
- Python操作Mysql实例代码教程在线版(查询手册)
本文介绍了Python操作MYSQL.执行SQL语句.获取结果集.遍历结果集.取得某个字段.获取表字段名.将图片插入数据库.执行事务等各种代码实例和详细介绍,代码居多,是一桌丰盛唯美的代码大餐 实 ...
- Java for LeetCode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Java for LeetCode 079 Word Search
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...