贪心 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 ...
随机推荐
- zstu.4019.排队购票(多维dp)
排队购票 Time Limit: 1 Sec Memory Limit: 64 MB Submit: 1264 Solved: 808 Description 一常球赛开始前,售票工作正在进行中. ...
- 解决win8下chrome浏览器打开提示没有注册类的方法
今天又把win8装回来了,继续装了个chrome浏览器,但是发现只能从安装的文件打开,从快捷方式或者快速启动栏都会提示没有注册类.找到一种解决的办法是删除注册表中的相关键值,具体如下: 1.打开注册表 ...
- Nginx安装与配置文件解析
导读 Nginx是一款开放源代码的高性能HTTP服务器和反向代理服务器,同时支持IMAP/POP3代理服务,是一款自由的软件,同时也是运维工程师必会的一种服务器,下面我就简单的说一下Nginx服务器的 ...
- DICOM:Ubuntu14环境下安装dcm4chee+oviyam2.1
背景: 之前博文DICOM:开源DICOM服务框架DCM4CHEE安装中曾介绍过在Windows7环境下安装部署dcm4chee.近期公司项目需要与部署到Linux系统的远端基于dcm4chee框架的 ...
- js指定标签的id只能添加不能删除
<body> <form id="form1" runat="server"> <div> <input id=&qu ...
- 数据库ACID
数据库的事务隔离级别 10.数据库的事务隔离级别一般分为4个级别,其中可能发生“不可重复读”的事物级别有()A.SERIALIZABLE B.READ COMMITTEDC.READ UNCOMMIT ...
- excel复制+粘贴,怎样让公式里的参数不自动变化?
例如,某一单元格内容为:=A1+A2 我把它复制+粘贴到其他地方,就自动变成了:=B1+B2 怎样让它不变化,仍保持=A1+A2 ?? 答: Excel一般使用相对地址来引用单元格的位置,当把一个含有 ...
- 【JAVA、C++】LeetCode 020 Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- Ubuntu下用命令行快速打开各类型文件
在Ubuntu下,通常用命令行打开文本文件,比如用命令gedit.more.cat.vim.less.但当需要打开其他格式文件时,比如pdf. jpg.mp3格式文件,咱们通常做法是进入到文件所在的目 ...
- LESS学习笔记1
个人理解:less是一个可以写函数的css