poj 1328 Radar Installation

3 篇文章 0 订阅

题意:给出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[N];
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值