贪心 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 ...
随机推荐
- Radar Installation(贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 56826 Accepted: 12 ...
- Windows 的 AD 域寄生于 Linux 机器
导读 对于帐户统一管理系统或软件来说,在 Linux 下你可能知道 NIS.OpenLDAP.samba 或者是 RedHat.IBM 的产品,在 Windows 下当然就是最出名的活动目录 (AD) ...
- C#内部类
http://blog.csdn.net/yanghua_kobe/article/details/6685222 在<Java编程思想>中花了一章的篇幅介绍,内部类的相关特性.而在C#的 ...
- django程序报错CSRF verification failed. Request aborted.
django程序的html页面中form的method='post'的时候报错 Forbidden (403) CSRF verification failed. Request aborted.He ...
- C++中的memset()函数 ------------转自:http://www.360doc.com/content/10/1006/18/1704901_58866679.shtml
memset()函数可以对大内存的分配进行很方便的操作(初始化),所谓“初始化”,当然是指将你定义的变量或申请的空间赋予你所期望的值,例如语句int i=0;就表明定义了一个变量i,并初始化为0:如果 ...
- System.SysUtils.TMarshaller 与 System.TMarshal
转自:http://www.cnblogs.com/del/archive/2013/06/10/3130974.html TMarshaller(结构) 基于 TMarshal(是有一大堆的 cla ...
- 深度学习入门教程UFLDL学习实验笔记二:使用向量化对MNIST数据集做稀疏自编码
今天来做UFLDL的第二个实验,向量化.我们都知道,在matlab里面基本上如果使用for循环,程序是会慢的一逼的(可以说基本就运行不下去)所以在这呢,我们需要对程序进行向量化的处理,所谓向量化就是将 ...
- Linux 系统安全 抵御TCP的洪水
抵御TCP的洪水 分类: LINUX tcp_syn_retries :INTEGER默认值是5对 于一个新建连接,内核要发送多少个 SYN 连接请求才决定放弃.不应该大于255,默认值是5,对应于1 ...
- 【SpringMVC】SpringMVC系列10之视图与视图解析器
10.视图与视图解析器 10.1.概述 请求处理方法执行完成后,最终返回一个 ModelAndView处理方法,Spring MVC 也会在内部将它们装配成一个ModelAndView 对象, ...
- Xenomai 安装准备工作
一些安装xenomai的参考资料: http://my.oschina.net/hevakelcj/blog/124290 http://blog.sina.com.cn/s/blog_60b9ee1 ...