贪心之建立雷达


如图所示,将问题转化为用最少的雷达点使得每条线段上至少有一个雷达

#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <memory>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
struct node
{
    int x,y;
    double left,right;
};
bool cmp(const node &a,const node &b)
{
    return a.left < b.left;
}
int n,d,kase=1;
node rader[1005];
int main()
{
    while(cin >> n >> d,n!=0&&d!=0)
    {
        int flag=1;
        for(int i=0; i<n; i++)
        {
            cin >> rader[i].x >> rader[i].y;
            if(abs(rader[i].y)>d)
            {
                flag=0;
            }
            rader[i].left = 1.0*rader[i].x - sqrt(1.0*(d*d-rader[i].y*rader[i].y));
            rader[i].right = 1.0*rader[i].x + sqrt(1.0*(d*d-rader[i].y*rader[i].y));
        }
        sort(rader,rader+n,cmp);
        int num = 1;
        double Left = rader[0].left,Right = rader[0].right;
        for(int i=1; i<n; i++)
        {
            if(rader[i].left <= Right)//注意此时right也需要更新
                {
                    Right = min(Right,rader[i].right);
                }
            else
            {
                num++;
                Right = rader[i].right;
            }
        }
        if(flag)
            cout<<"Case"<<' '<<kase<<": "<<num<<endl;
        else
            cout<<"Case"<<' '<<kase<<": "<<-1<<endl;
        kase++;
        getchar();
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值