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 ...
随机推荐
- OWIN是Open Web Server Interface for .NET
http://owin.org/ Servers and Hosts Katana Nowin Suave Frameworks Jasper Nancy SignalR WebApi WebShar ...
- Hive框架基础(二)
* Hive框架基础(二) 我们继续讨论hive框架 * Hive的外部表与内部表 内部表:hive默认创建的是内部表 例如: create table table001 (name string , ...
- java反射与多态(父类调用子类)的代码演示
package Test0817; import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method ...
- CMD和AMD的区别
CMD和AMD俩者之间的区别 AMD和CMD最大的区别是对依赖模块的执行时机处理不同 CMD和AMD都是CommonJS延伸而来的,CommonJS是随着node的出现而出现的,它是一个规范,用于定义 ...
- 记intel杯比赛中各种bug与debug【其二】:intel caffe的使用和大坑
放弃使用pytorch,学习caffe 本文仅记录个人观点,不免存在许多错误 Caffe 学习 caffe模型生成需要如下步骤 编写network.prototxt 编写solver.prototxt ...
- Linux学习-Ubuntu 18.04-安装图文教程
Ubuntu(友帮拓.优般图.乌班图)是一个以桌面应用为主的开源GNU/Linux操作系统,Ubuntu 是基于Debian GNU/Linux,支持x86.amd64(即x64)和ppc架构,由全球 ...
- 使用GitHub+Hexo建立个人网站,并绑定自己的域名(Ubuntu环境下)
参考链接: youngzn.github.io hexo官网 博客:从jekyll到hexo hexo建站小结 全过程 简洁过程 使用GitHub+Hexo建立个人网站,并绑 ...
- Unity 获得视频的某一帧,生成缩略图
Unity 并无直接获取视频某一帧图像的API,所以想要生成缩略图就要自己写方法了, 图片和视频都可以用这种方式生成缩略图,另,转载请标明出处,谢谢. using System.Collections ...
- C语言函数--E
函数名: ecvt 功 能: 把一个浮点数转换为字符串 用 法: char ecvt(double value, int ndigit, int *decpt, int *sign); 程序例: #i ...
- openGl超级宝典学习笔记 (2) 7个主要的几何图元
点(GL_POINTS): 点总是正方形的像素,默认情况下,点的大小不受透视除法影响. 即无论与视点的距离怎样,它的大小都不改变.为了获得圆点.必须在抗锯齿模式下绘制点. 能够用glPointSize ...