POJ 1328 Radar Installation 贪心(区间选点)

14 篇文章 0 订阅
区间选点问题,坑点 一个是double类型,2是y > d|| d < 0要考虑到,3是要按照左端点进行升序排序,4是算法核心内容,如果选取的cou作为雷达位置,那么从下一个区间开始选点,如果下一个区间的右端点比小于等于cou,那么让cou = 这个区间的右端点,如果下一个区间的左端点大于cou,那么再重新安装一个雷达,让雷达安放在这个区间的右端点上。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <math.h>

using namespace std;

struct node
{
    double u;
    double v;
}a[1123];

bool cmp(node a, node b)
{
    return a.u < b.u;
}

int main()
{
    int n, d;
    int i;
    double x, y;
    int count = 1;
    while(cin >> n >> d){
        if(n == 0 && d == 0)
            break;
        int flag = false;
        for(i = 0;i < n;i++){
            cin >> x >> y;
            if(y > d || d < 0){
                flag = true;
            }
            a[i].u = x-sqrt(d*d - y*y);
            a[i].v = x+sqrt(d*d - y*y);
        }
        if(!flag){
            int cnt;
            double cou;
            sort(a,a+n,cmp);
            cnt = 1;
            cou = a[0].v;
            for(i = 1;i < n;i++){
                if(a[i].v <= cou){
                    cou = a[i].v;
                }else if(a[i].u > cou){
                    cnt++;
                    cou = a[i].v;
                }
            }
            printf("Case %d: %d\n", count, cnt);
        }else {
            printf("Case %d: -1\n", count);
        }
        count++;
    }
    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值