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 ...
随机推荐
- [luoguP4302] [SCOI2003] 字符串折叠 解题报告(区间DP)
题目链接:https://www.luogu.org/problemnew/show/P4302 题解: 我们考虑折叠一个区间里的字符串,怎么样才是最优的 1.把这个区间分成几部分分别折叠 2.把这个 ...
- Laravel-路由组和中间件
Laravel-路由组和中间件 标签(空格分隔): php 定义路由组 Route::group(['prefix'=>'Anime'], function(){ Rout::match(['g ...
- 插入记录INSERT(二十五)
插入记录INSERT 我们先来看第一个操作:INSERT 实际上在mysql当中一共存在着3种不同的insert语句,我们先来看第一种.它的语法结构如下: 一.插入记录 INSERT [INTO] t ...
- <Sicily>Tiling a Grid With Dominoes
一.题目描述 We wish to tile a grid 4 units high and N units long with rectangles (dominoes) 2 units by on ...
- logsource and ALO
1.首先配置sourcedb上的nfs服务,oggstd上挂载sourcedb的online redo和archive log的目录 oggsource上配置: vi /etc/export ...
- CF 986C AND Graph(建模+DFS)
#include<stdio.h> ],v[]; ],n,al; void dfs(int x){ if(v[x])return; v[x]=; if(ex[x])dfs(al^x); ; ...
- PostgreSQL指定用户可访问的数据库pg_hba.conf
进入指定目录: # cd /var/lib/pgsql/9.3/data/ 使用vi编辑pg_hba.conf文件 # vi pg_hba.conf 以上配置为所有IP及网关都允许访问,使用MD5认证 ...
- Spring配置方式
Spring配置方式 第一阶段:xml配置 在spring 1.x时代,使用spring开发满眼都是xml配置的bean,随着项目的扩大, 我们需要把xml配置文件分放到不同的配置文件中,那时 ...
- CSU 1446 Modified LCS 扩展欧几里得
要死了,这个题竟然做了两天……各种奇葩的错误…… HNU的12831也是这个题. 题意: 给你两个等差数列,求这两个数列的公共元素的数量. 每个数列按照以下格式给出: N F D(分别表示每个数列的长 ...
- C# DataTable中按字符串中的数字排序
例如datatable中有一列是门牌号格式是xx-xx-xx,或字符串中含有汉字或其他符号等等,如何按照正确的数字顺序排序呢? 1.获得字符串中的数字. 2.在datatable中添加一列,类型是In ...