POJ 1328 Radar Installation【贪心】
题意:
将一条海岸线看成X轴,X轴上面是大海,海上有若干岛屿,给出雷达的覆盖半径和岛屿的位置,要求在海岸线上建雷达,在雷达能够覆盖全部岛屿情况下,求雷达的最少使用量。
分析:
贪心法,先研究一下每个岛屿,设岛屿到海岸线的垂直距离为d,雷达的覆盖半径为k,若d>k,直接输出-1,若d<=k,则雷达的建造有一个活动区间[x1,x2](用平面几何可以求得出来)。因此,在可以覆盖的情况下每个岛屿都有一个相应的活动区间。该问题也就转变成了最少区间选择问题即:
在n个区间中选择一个区间集合,集合中的各个区间都不相交,集合中元素的个数就是答案了。
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
using namespace std; struct point
{
double left, right;
}p[2010], temp; bool operator < (point a, point b)
{
return a.left < b.left;
} int main()
{
int n;
double r;
int kase = 0;
while(true)
{
scanf("%d%lf",&n,&r);
if(n==0&&r==0)break;
bool flag = false;
for (int i = 0; i < n; i++)
{
double a, b;
scanf("%lf%lf",&a,&b);
if (fabs(b) > r)
{
flag = true;
}
else
{
p[i].left = a * 1.0 - sqrt(r * r - b * b);
p[i].right = a * 1.0 + sqrt(r * r - b * b);
}
}
cout << "Case " << ++kase << ": ";
if (flag)
{
cout << -1 << endl;
}
else
{
int count = 1;
sort(p, p + n);
temp = p[0]; for (int i = 1; i < n; i++)
{
if (p[i].left > temp.right)//不重叠
{
count++;
temp = p[i];
}
else if (p[i].right < temp.right)//重叠取里面的端点
{
temp = p[i];
}
}
cout << count << endl;
}
}
return 0;
}
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(贪心+快排)
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- POJ - 1328 Radar Installation(贪心区间选点+小学平面几何)
Input The input consists of several test cases. The first line of each case contains two integers n ...
- 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 贪心 难度:1
http://poj.org/problem?id=1328 思路: 1.肯定y大于d的情况下答案为-1,其他时候必定有非负整数解 2.x,y同时考虑是较为麻烦的,想办法消掉y,用d^2-y^2获得圆 ...
- poj 1328 Radar Installation(贪心)
题目:http://poj.org/problem?id=1328 题意:建立一个平面坐标,x轴上方是海洋,x轴下方是陆地.在海上有n个小岛,每个小岛看做一个点.然后在x轴上有雷达,雷达能覆盖的范 ...
- POJ 1328 Radar Installation 贪心题解
本题是贪心法题解.只是须要自己观察出规律.这就不easy了,非常easy出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 依照x轴大小排序 2 从最左边的点循环.首先找到最小x轴的圆 ...
- POJ 1328 Radar Installation#贪心(坐标几何题)
(- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<algorithm> #include<cmath ...
- 贪心 POJ 1328 Radar Installation
题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...
- poj 1328 Radar Installation (简单的贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42925 Accepted: 94 ...
随机推荐
- ThreadLocal以及内存泄漏
ThreadLocal是什么 ThreadLocal 的作用是提供线程内的局部变量,这种变量在线程的生命周期内起作用,减少同一个线程内多个函数或者组件之间一些公共变量的传递的复杂度.但是如果滥用Thr ...
- 个股和股票池的beta系数的估算
个股的beta系数的估算 代码 def test_gg_beta(symbol='000895.sz', start='2018-01-01', plot_price=True, align_to=' ...
- 调用write()写
一.在POSIX中的定义 #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count); 二.返回值 ( ...
- 【51nod】1239 欧拉函数之和 杜教筛
[题意]给定n,求Σφ(i),n<=10^10. [算法]杜教筛 [题解] 定义$s(n)=\sum_{i=1}^{n}\varphi(i)$ 杜教筛$\sum_{i=1}^{n}(\varph ...
- VS2012 安装番茄插件
1.visual assist x 1929_大番茄.rar 下载地址: http://pan.baidu.com/s/1qXOUuJE 网盘提取密码: 3mka 文件解压密码: eg5p 2.安装完 ...
- 蒙特卡罗定位(Particle Filter Localization)笔记
善始善终,这篇文章是Coursera课程Robotics: Estimation and Learning最后一周的课程总结.里面的小哥讲得不是很清晰,留下的作业很花功夫(第二周课程也是酱紫). 这周 ...
- mysql系列十、mysql索引结构的实现B+树/B-树原理
一.MySQL索引原理 1.索引背景 生活中随处可见索引的例子,如火车站的车次表.图书的目录等.它们的原理都是一样的,通过不断的缩小想要获得数据的范围来筛选出最终想要的结果,同时把随机的事件变成顺序的 ...
- windows使用python原生组件包获取系统日志信息
#coding=utf8 import sys import traceback import win32con import win32evtlog import win32evtlogutil i ...
- C/C++:函数调用规则__stdcall,__cdecl,__pascal,__fastcall
__cdecl __cdecl 是 C Declaration 的缩写,表示 C 语言默认的函数调用方法:所有参数从右到左依次入栈,这些参数由调用者清除,称为手动清栈.被调用函数不会要求调用者传递多 ...
- 006_ssl监测及评分
https://testssl.sh/ 一. https://www.ssllabs.com/ssltest/analyze.html?d=jyall.com 监测下jyll.com,不忍直视啊! 二 ...