【贪心】POJ1328-Radar Installation
【思路】
以每一座岛屿为圆心,雷达范围为半径作圆,记录下与x轴的左右交点。如果与x轴没交点,则直接退出输出“-1”。以左交点为关键字进行排序,从左到右进行贪心。容易知道,离每一个雷达最远的那一座岛与雷达相距恰巧为半径的时候,可以得到最优解。假设上一个雷达与第before座岛相距为半径大小,对于当前的岛屿i:
如果before岛的右交点在i岛左交点的左侧,此时必然需要一个新的雷达,将当前before暂定为i,雷达数加一;
如果before岛的右交点在i岛右交点的右侧,为了让雷达举例尽可能地广,将雷达移至当前岛屿与x轴的交点上,将当前before暂定为i,雷达数不变。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
double sqrt(double n);
struct rec
{
double left,right;
bool operator < (const rec& x) const
{
return left<x.left;
}
}; const int MAXN=+;
int n,d;
rec inter[MAXN]; int calculate()
{
sort(inter,inter+n);
int ans=;
int before=;
for (int i=;i<n;i++)
{
if (inter[before].right<inter[i].left)
{
ans++;
before=i;
}
else if (inter[before].right>=inter[i].right)
{
before=i;
}
}
return ans;
} int main()
{
int t=;
while (scanf("%d%d",&n,&d))
{
if (n==d && d==) break;
t++;
int f=;
for (int i=;i<n;i++)
{
double x,y;
cin>>x>>y;
if (d<y)
{
f=;
}
else
{
inter[i].left=x*1.0-sqrt(d*d-y*y);
inter[i].right=x*1.0+sqrt(d*d-y*y);
}
}
cout<<"Case "<<t<<": ";
if (f) cout<<calculate()<<endl;else cout<<-<<endl; }
return ;
}
【贪心】POJ1328-Radar Installation的更多相关文章
- [POJ1328]Radar Installation
[POJ1328]Radar Installation 试题描述 Assume the coasting is an infinite straight line. Land is in one si ...
- POJ--1328 Radar Installation(贪心 排序)
题目:Radar Installation 对于x轴上方的每个建筑 可以计算出x轴上一段区间可以包含这个点 所以就转化成 有多少个区间可以涵盖这所有的点 排序之后贪心一下就ok 用cin 好像一直t看 ...
- POJ1328 Radar Installation 【贪心·区间选点】
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 54593 Accepted: 12 ...
- POJ1328——Radar Installation
Radar Installation Description Assume the coasting is an infinite straight line. Land is in one side ...
- POJ1328 Radar Installation 解题报告
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- 贪心——D - Radar Installation
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...
- 贪心 + 计算几何 --- Radar Installation
Radar Installation Description Assume the coasting is an infinite straight line. Land is in one side ...
- poj1328 Radar Installation(贪心 策略要选好)
https://vjudge.net/problem/POJ-1328 贪心策略选错了恐怕就完了吧.. 一开始单纯地把island排序,然后想从左到右不断更新,其实这是错的...因为空中是个圆弧. 后 ...
- POJ1328 Radar Installation(贪心)
题目链接. 题意: 给定一坐标系,要求将所有 x轴 上面的所有点,用圆心在 x轴, 半径为 d 的圆盖住.求最少使用圆的数量. 分析: 贪心. 首先把所有点 x 坐标排序, 对于每一个点,求出能够满足 ...
- zoj1360/poj1328 Radar Installation(贪心)
对每个岛屿,能覆盖它的雷达位于线段[x-sqrt(d*d-y*y),x+sqrt(d*d+y*y)],那么把每个岛屿对应的线段求出来后,其实就转化成了经典的贪心法案例:区间选点问题.数轴上有n个闭区间 ...
随机推荐
- vue双向绑定原理源码解析
当我们学习angular或者vue的时候,其双向绑定为我们开发带来了诸多便捷,今天我们就来分析一下vue双向绑定的原理. 简易vue源码地址:https://github.com/maxlove123 ...
- Gulp、Grunt构建工具
在Gulp中创建一个库从磁盘gulp.src读取源文件并通过磁盘管道写回内容到gulp.dest,可以理解成只是将文件复制到另一个目录. var gulp = require('gulp'); gul ...
- SpringBoot工程目录配置
Spring Boot建议的目录结果如下: root package结构:com.example.myproject com +- example +- myproject +- Applicat ...
- Python3 文件基本操作
Python文件的打开模式有: r,只读模式(默认).w,只写模式.[不可读:不存在则创建:存在则删除内容:]a,追加模式.[可读: 不存在则创建:存在则只追加内容:]"+" 表示 ...
- linux非阻塞的socket EAGAIN的错误处理【转】
转自:http://blog.csdn.net/tianmohust/article/details/8691644 版权声明:本文为博主原创文章,未经博主允许不得转载. 在Linux中使用非阻塞的s ...
- (十五)linux下gdb调试
一.gdb常用命令: 命令 描述 backtrace(或bt) 查看各级函数调用及参数 finish 连续运行到当前函数返回为止,然后停下来等待命令 frame(或f) 帧编号 选择栈帧 info(或 ...
- SAE如何使用Git
了解Git及远程git仓库 请先看博文<Git入门及上传项目到github中>,弄懂了之后我相信我下面说的就相当于废话了. SAE的git远程仓库就相当于github. 向SAE的远程仓库 ...
- vue知识点(1)
处理用户输入 v-on指令添加一个事件监听器 div id="app-5"> <p>{{ message }}</p> <button v-on ...
- 2.Python3标准库--文本
(一)string:文本常量和模板 1.函数 import string ''' string模块在最早的Python版本中就已经有了.以前这个模块中提供的很多函数已经移植到str对象中,不过这个模块 ...
- 【python】pymongo中正则查询时的转义问题
在查询mongo时用到了正则查询 设字符串为 str = '/ab/cd.ef?g=' 直接用正则查询没有匹配. collection.find({"re":{'$regex' ...