您的位置:首页 > 其它

poj 1328 Radar Installation

2016-09-25 23:20 323 查看
题意:给出n个点和圆的半径d,在x轴上放置圆(圆心在x轴上),问最少要放多少个圆才能把所有点覆盖?如果不能全部覆盖输出 -1。点都在x轴上方。

分析:当有 yi > d 的时候,输出 -1,否则把所有点排序,然后从左往右贪心放圆即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

#define x first
#define y second
typedef pair<double, double> point;
const int N = 2000;
point p
;
int n, d;

int main() {
//freopen("in.txt", "r", stdin);
int cas = 0;
while(scanf("%d%d", &n, &d) != EOF) {
if(n == 0 && d == 0) break;
bool nosul = false;
for(int i = 0; i < n; i++) {
scanf("%lf%lf", &p[i].x, &p[i].y);
if(p[i].y > d) {
nosul = true;
}
}

printf("Case %d: ", ++cas);
if(nosul) {
puts("-1");
}
else {
sort(p, p + n);
int ans = 1;
double tmp = sqrt(d * d - p[0].y * p[0].y);
double rx = p[0].x + tmp;
for(int i = 1; i < n; i++) {
tmp = sqrt(d * d - p[i].y * p[i].y);
double tmplx = p[i].x - tmp;
double tmprx = p[i].x + tmp;
/*
这样判也可以,不过会被卡精度,两种判法本质上一样
rx = min(rx, tmprx);
if((p[i].x - rx) * (p[i].x - rx) + p[i].y * p[i].y
> d * d + 1e-6) {
ans++;
rx = tmprx;
}
*/
if(tmplx > rx) {
ans++;
rx = tmprx;
}
else {
rx = min(rx, tmprx);
}
}
printf("%d\n", ans);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj