POJ 1328 Radar Installation 贪心题解
本题是贪心法题解。只是须要自己观察出规律。这就不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 贪心题解的更多相关文章
- 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(贪心区间选点+小学平面几何)
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 贪心算法
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#贪心(坐标几何题)
(- ̄▽ ̄)-* #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 ...
随机推荐
- 36.创建自定义的指令directive
转自:https://www.cnblogs.com/best/tag/Angular/ 1. <html> <head> <meta charset="utf ...
- 《转载》编程入门指南 v1.4
编程入门指南 v1.4 Badger · 8 个月前 作者:@萧井陌, @Badger 自由转载-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 3.0 CoCode ...
- ubuntu -redis
ubentu 布置redis,基本操作和CentO感觉相差不多,主要是使用命令有所差异 mark如下: ① download ② tar -zxvf xxx.tar.gz ③ cd redis-xxx ...
- 【J-meter】调试JDBC请求
参考资料: http://www.codesec.net/view/165234.html
- 洛谷——P1970 花匠
https://www.luogu.org/problem/show?pid=1970 题目描述 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定 把这排中的一部分花移走, ...
- Android——bootchart
bootchart:android原生自带的开机性能查看机制.通过收集android开机过程中的各种log数据,终于能够图表的形式展现各个进程在开机过程中的性能.(博客不能断-) 撰写不易,转载需注明 ...
- 【Oracle错误集锦】:ORA-00119 & ORA-00132
有时候老天就是爱和你开玩笑,昨天好不easy配置好Oracle.可以用PL/SQL正常登录使用,今天突然就不行了.而且错误十分诡异,没有提示什么错误代码.输入usernamepassword,点击登录 ...
- 实战Jquery(四)--标签页效果
这两天完毕了实战四五六的样例,实例四是标签页的实现方法,实例五是级联菜单下拉框,实例六是窗体效果,都是web层经常使用的效果.越到后面越发认为技术这东西,就是一种思路的展现,懂了要实现 ...
- java 处理word文档 (含图片,表格内容)
因为本人长期从事Oa相关项目的开发,所以处理word文档,Pdf,Excel等是在所难免的. 1.需求 处理Excel 能够用jxl 或者poi 2需求 用户在系统上填 ...
- Android开发之蓝牙(Bluetooth)操作(一)--扫描已经配对的蓝牙设备
版权声明:本文为博主原创文章,未经博主允许不得转载. 一. 什么是蓝牙(Bluetooth)? 1.1 BuleTooth是目前使用最广泛的无线通信协议 1.2 主要针对短距离设备通讯(10m) ...