poj1328 Radar Installation(贪心)

poj1328

题目

http://poj.org/problem?id=1328
就是告诉你几个小岛的坐标,告诉你雷达的覆盖半径,在x轴上建造雷达,求最小的数量覆盖所有小岛。

思路

以每个小岛为圆心,作雷达半径的圆,与x轴的交点就是能够覆盖这个小岛的雷达x值的范围,然后从第一个小岛开始,如果下一个小岛的区间和当前的有交集,则缩小区间,看下一个,如果没交集,就更新,增加雷达个数。

代码

#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <iostream>
#include <cstring>
#include <cmath>

using namespace std;
typedef long long int ll;

struct node
{
    double l,r;
    bool operator <(const node a)const
    {
        return a.l>l;
    }
} area[1010];


int cnt;
int n,d;
int flag;

void f(double a,double b)
{
    if(abs(b)>d) flag=1;
    else
    {
        area[cnt].l=a-sqrt(1.0*d*d-b*b);
        area[cnt].r=a+sqrt(1.0*d*d-b*b);
        cnt++;
    }
}

int main()
{
    int kase=1;
    while(scanf("%d %d",&n,&d))
    {
        if(n==0&&d==0) break;
        flag=0;
        cnt=0;
        for(int i=0; i<n; i++)
        {
            double a,b;
            scanf("%lf %lf",&a,&b);
            f(a,b);
        }
        if(flag==1)
        {
            printf("Case %d: -1\n",kase++);
            continue;
        }
        int ans=1;
        sort(area,area+n);
        double nowl=area[0].l;
        double nowr=area[0].r;
        for(int i=1; i<cnt; i++)
        {
            if(area[i].l<=nowr&&area[i].l>=nowl)
            {
                nowl=area[i].l;
                nowr=min(nowr,area[i].r);
            }
            else
            {
                ans++;
                nowl=area[i].l;
                nowr=area[i].r;
            }
        }
        printf("Case %d: %d\n",kase++,ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值