本题是贪心法题解。只是须要自己观察出规律。这就不easy了,非常easy出错。

一般网上做法是找区间的方法。

这里给出一个独特的方法:

1 依照x轴大小排序

2 从最左边的点循环。首先找到最小x轴的圆

3 以这个圆推断能够包含右边的多少个圆,直到不能够包含下一个点,那么继续第2步,画一个新圆。

看代码吧,应该非常清晰直观的了。

效率是O(n),尽管有嵌套循环。可是下标没有反复。一遍循环就能够了。故此是O(n)。

#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <float.h>
using namespace std;
const int MAX_N = 1001;
inline float MAX(float a, float b) { return a > b ? a : b; }
inline float MIN(float a, float b) { return a < b ? a : b; } struct Point
{
float x, y;
bool operator<(const Point &p) const
{
if (x == p.x) return y > p.y;
return x < p.x;
}
float dist(const Point &p) const
{
float a = (x - p.x);
float b = (y - p.y);
return sqrtf(a*a+b*b);
}
float dist(const float x1, const float y1) const
{
float a = (x - x1);
float b = (y - y1);
return sqrtf(a*a+b*b);
}
}; Point ps[MAX_N]; int calRadar(int n, int d)
{
sort(ps, ps+n);
float cenX = 0.0f, cenY = 0.0f;
int ans = 0;
for (int i = 0; i < n; ) //视情况而添加i
{
ans++;
float dx = sqrtf(float(d*d) - ps[i].y*ps[i].y);
cenX = ps[i].x + dx; for (i++; i < n && ps[i].x <= cenX; i++)
{
dx = sqrtf(float(d*d) - ps[i].y*ps[i].y);
cenX = MIN(cenX, ps[i].x + dx);
} float dis = 0.0f;
for ( ; i < n; i++)
{
dis = ps[i].dist(cenX, cenY);
if (dis > float(d)) break;
}
}
return ans;
} int main()
{
int n, d, t = 1; while (scanf("%d %d", &n, &d) && (n || d))
{
float maxY = FLT_MIN;//这个放外面了,错误! for (int i = 0; i < n; i++)
{
scanf("%f %f", &ps[i].x, &ps[i].y);
maxY = MAX(ps[i].y, maxY);
}
if (maxY > (float)d) printf("Case %d: -1\n", t++);
else printf("Case %d: %d\n", t++, calRadar(n, d));
}
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(贪心区间选点+小学平面几何)

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

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

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

  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#贪心(坐标几何题)

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

  8. 贪心 POJ 1328 Radar Installation

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

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

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

随机推荐

  1. action support分析

    Action这一部分主要是数据(索引)的操作和部分集群信息操作. 所有的请求通过client转发到对应的action上然后再由对应的TransportAction来执行相关请求.如果请求能在本机上执行 ...

  2. nyoj--767--因子和(模拟)

    因子和 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述 题目很简单明了,给你一个数n,判断它是不是素数,如果是素数就输出"Yes",如果不是素数则输出 ...

  3. windows下git的安装和使用

    git到底是个什么东西,我这里就不介绍了,如果大家还有不懂的,可以去百度一下的.我这里给一个介绍的网址:git简介        这里在留一个地址http://baike.baidu.com/subv ...

  4. Django关于图片验证码显示笔记

    .访问页面 /login/ - 内部需要创建一张图片,并且给用户返回 - 创建一个白板 Session存放验证码 .POST - 根据用户提交的数据与session里面比较 .登录界面 和 验证码 分 ...

  5. leetcode 新题型----SQL,shell,system design

    leetcode 主要是一个针对北美的coder人群找工作的代码练习网站,我在2015年初次接触这个网站的时候,总共只有200多道题目,是一个类似acm 的a题网站.这些年变化越来越大,主要是因为找工 ...

  6. B-Boxes

    http://agc010.contest.atcoder.jp/tasks/agc010_b Problem Statement There are N boxes arranged in a ci ...

  7. 我的 Linux 主目录中的隐藏文件是干什么用的?

    作者: Alexander Fox 译者: LCTT MjSeven 在 Linux 系统中,你可能会在主目录中存储了大量文件和文件夹.但在这些文件之外,你知道你的主目录还附带了很多隐藏的文件和文件夹 ...

  8. 一篇文章助你理解Python2中字符串编码问题

    前几天给大家介绍了unicode编码和utf-8编码的理论知识,没来得及上车的小伙伴们可以戳这篇文章:浅谈unicode编码和utf-8编码的关系.下面在Python2环境中进行代码演示,分别Wind ...

  9. 首家5G体验厅在深圳建成

    日前,深圳移动卓越时代营业厅推出5G全方位体验活动,让市民亲身感受5G时代到来.据悉,十大5G展示项目生动展现移动5G带来的生活巨变与产业升级,为5G发展汇聚各界力量加速创新落地. 现场有市民表示,5 ...

  10. unity 自动删除未引用的Assets下的资源

    随着时间的堆积,项目中Assets文件夹下的资源会变得越来越繁杂,有些贴图.材质啥的可能压根没有使用过,但是又不敢轻易去删除. 这里分享两个插件,用于管理这些资源. 一.ResourceChecker ...