贪心 POJ 1328
来源:互联网 发布:eviews非线性回归算法 编辑:程序博客网 时间:2024/06/03 21:55
题意:假设海岸线是一条无限延伸的直线。每一个小的岛屿是海洋上的一个点。雷达坐落于海岸线上,只能覆盖d距离,所以如果小岛能够被覆盖到的话,它们之间的距离最多为d。
题目要求计算出能够覆盖给出的所有岛屿的最少雷达数目。
一目了然,求投影到X轴上,然后重叠位置只记一次,有两种情况:当此时的线段左端点大于此时比较的右端点,不会有重合了,更新;当当此时的线段右端点小于此时比较的右端点,范围更小了,更新比较线段(这样做都是建立在sort之后,每一线段的左端点不减)
#include <iostream> #include <algorithm> #include <stdlib.h> #include <math.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 (cin >> n >> r && (n || r)) { bool flag = false; for (int i = 0; i < n; i++) { double a, b; cin >> 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 countt = 1; sort(p, p + n); temp = p[0]; for (int i = 1; i < n; i++) { if (p[i].left > temp.right) { countt++; temp = p[i]; } else if (p[i].right < temp.right) { temp = p[i]; } } cout << countt << endl; } } }
0 0
- POJ 1328 贪心
- poj 1328 贪心
- Poj 1328 贪心
- POJ 1328(贪心)
- poj 1328 贪心
- POJ 1328 贪心
- POJ-1328(贪心算法)
- POJ 1328 贪心算法
- poj.1328贪心
- poj 1328 贪心
- poj 1328 贪心
- poj 1328 贪心 水题
- poj 1328(贪心)
- POJ 1328(贪心)
- poj 1328 贪心
- POJ 1328(排序+贪心)
- poj 1328 贪心
- POJ 1328 贪心
- Mac OS 安装PHP7
- Android开发整理
- 轮播图动态设置高度保持图片不变形
- 递归实现的 汉诺塔
- JHTP小结_第十三章_图形及Java2D
- 贪心 POJ 1328
- vb6.0 office excel 和wps共存,创建正确的excel对象以便操作excel表格
- java内存溢出和内存泄露
- hibernate属性映射出现could not resolve property问题
- shell脚本常用脚本
- 配置gitconfig中git命令行的简写
- 正则表达式
- poj 1383 Labyrinth (迷宫 最长路)
- Eclipse 下如何引用另一个项目的Java文件