贪心 POJ 1328
2016-08-02 23:34
274 查看
题意:假设海岸线是一条无限延伸的直线。每一个小的岛屿是海洋上的一个点。雷达坐落于海岸线上,只能覆盖d距离,所以如果小岛能够被覆盖到的话,它们之间的距离最多为d。
题目要求计算出能够覆盖给出的所有岛屿的最少雷达数目。
一目了然,求投影到X轴上,然后重叠位置只记一次,有两种情况:当此时的线段左端点大于此时比较的右端点,不会有重合了,更新;当当此时的线段右端点小于此时比较的右端点,范围更小了,更新比较线段(这样做都是建立在sort之后,每一线段的左端点不减)
题目要求计算出能够覆盖给出的所有岛屿的最少雷达数目。
一目了然,求投影到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; } } }
相关文章推荐
- poj1328 Radar Installation(贪心)
- poj 1328 贪心
- 贪心-poj-1328-Radar Installation
- poj1328贪心
- Radar Installation(POJ 1328)(贪心)
- A - Radar Installation POJ - 1328 (区间贪心)
- POJ_1328 Radar Installation(贪心)
- poj 1328 贪心
- Radar Installation POJ - 1328 贪心
- poj1328贪心
- POJ 1328 Radar Installation(雷达安装)__贪心
- POJ 1328 Radar Installation(贪心)
- poj 1328 Radar Installation(贪心)
- POJ 1328 Radar Installation(贪心)
- poj 1328 贪心 水题
- poj1328贪心
- POJ 1328 Radar Installation 贪心
- poj 1328 Radar Installation【贪心】【刷题计划】
- POJ1328 Radar Installation 【贪心·区间选点】
- poj1328 区间贪心 雷达覆盖岛屿