POJ-1328 Radar Installation 贪心
以每个点算出左右覆盖的雷达所在x轴范围,然后贪心计算出所需圆的个数。
当后一个点的圆心在x轴的左坐标在前一个点的右坐标的右坐标之前,则这个点就会被覆盖。
代码如下:(C++能过,G++ runtime error)
#include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <algorithm> using namespace std; int N, R; struct Node { double l, r; bool operator < (Node t) const { if (r != t.r) { return t.r > r; } } }e[1010]; int deal() { int ans = 1; double pos = e[1].r; for (int i = 2; i <= N; ++i) { if (e[i].l > pos) { ++ans; pos = e[i].r; } } return ans; } int main() { int x, y, flag, ca = 0; while (scanf("%d %d", &N, &R), N|R) { flag = 0; for (int i = 1; i <= N; ++i) { scanf("%d %d", &x, &y); if (y > R) { flag = 1; } else { double dis = sqrt(double(R*R-y*y); e[i].l = x - dis; e[i].r = x + dis; } } printf("Case %d: ", ++ca); if (flag) { puts("-1"); } else { sort(e+1, e+1+N); int t = deal(); printf("%d\n", t); } } return 0; }