POJ 1328

题意:

将一条海岸线看成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【贪心】的更多相关文章

  1. POJ 1328 Radar Installation 贪心 A

    POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...

  2. poj 1328 Radar Installation(贪心+快排)

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  3. POJ - 1328 Radar Installation(贪心区间选点+小学平面几何)

    Input The input consists of several test cases. The first line of each case contains two integers n ...

  4. POJ 1328 Radar Installation 贪心算法

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  5. POJ 1328 Radar Installation 贪心 难度:1

    http://poj.org/problem?id=1328 思路: 1.肯定y大于d的情况下答案为-1,其他时候必定有非负整数解 2.x,y同时考虑是较为麻烦的,想办法消掉y,用d^2-y^2获得圆 ...

  6. poj 1328 Radar Installation(贪心)

    题目:http://poj.org/problem?id=1328   题意:建立一个平面坐标,x轴上方是海洋,x轴下方是陆地.在海上有n个小岛,每个小岛看做一个点.然后在x轴上有雷达,雷达能覆盖的范 ...

  7. POJ 1328 Radar Installation 贪心题解

    本题是贪心法题解.只是须要自己观察出规律.这就不easy了,非常easy出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 依照x轴大小排序 2 从最左边的点循环.首先找到最小x轴的圆 ...

  8. POJ 1328 Radar Installation#贪心(坐标几何题)

    (- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<algorithm> #include<cmath ...

  9. 贪心 POJ 1328 Radar Installation

    题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...

  10. poj 1328 Radar Installation (简单的贪心)

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42925   Accepted: 94 ...

随机推荐

  1. POJ 2127 Greatest Common Increasing Subsequence

    You are given two sequences of integer numbers. Write a program to determine their common increasing ...

  2. Zabbix LLD 设置过滤条件,不自动监控某些item

    1.需求描述        默认情况下Zabbix 自带模板 "Template OS Linux" 中网络接口LLD自动发现除还回接口外的所有接口,当这并不一定是我们想要的结果. ...

  3. Python HTML操作(HTMLParser)

    HTML操作是编程中很重要的一块,下面用Python3.x中的html.parser中的HTMLParser类来进行HTML的解析. HTMLParser类定义及常用方法 标准库中的定义 class ...

  4. DBeaver入门

    1 安装好连接好数据库,查询操作 注意黄色字体1 2 3 4 执行sql操作

  5. cdn.dns,cms

    CDN CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.通过在网络各 ...

  6. php 匹配替换中文

    1.匹配中文 $str = "中文“; preg_match_all("/[\x{4e00}-\x{9fa5}]+/u",$str,$match); 2.替换中文: 在所 ...

  7. ODPS

    ODPS 功能之概述篇 原文  http://blog.aliyun.com/2962 主题 SQL 概述 ODPS是阿里云基于自有的云计算技术研发一套开放数据处理服务(Open Data Proce ...

  8. Latex 公式居中

    这么简单的功能要是还要加工具包,LaTeX也不用混了~ 公式用\[...\]来写可以达到公式居中效果. 或者在equation环境外加center环境: \begin{center} \begin{e ...

  9. MAC上mongodb连接不上

    1.在Mac客户端里输入 mongo,发现mongo连接不上了,原因是mongo的服务没有开启. 2.在命令行了输入 mongod,开启服务的命令 3.启动起来以后,用mongo连接服务器.

  10. 【转】SSH服务详解

    [转]SSH服务详解 第1章 SSH服务 1.1 SSH服务协议说明 SSH 是 Secure Shell Protocol 的简写,由 IETF 网络工作小组(Network Working Gro ...