POJ 1328 Radar Installation

POJ 1328 Radar Installation

[★★☆☆☆]贪心

  • 题目大意:

    平面上有一些点,现要求用一些圆心在x轴上的圆(雷达)来覆盖这些点,问最少需要多少雷达。

  • 输入格式:

    N(N个小岛)d (雷达覆盖范围)
    N行:xi, yi(小岛坐标)

  • 输出格式:

    case # :最小雷达个数

  • 样例

    输入:
    3 2
    1 2
    -3 1
    2 1

    1 2
    0 2

    0 0

    输出:
    Case 1: 2
    Case 2: 1

  • 解题思路:

    贪心算法,比较直接就能想到。
    根据小岛的左边算出可以安装雷达的区间。
    最开始想的是按照区间左边排序,后来发现并不对。。要按照右边排序再跟左边的值进行比较。
    但是这到底我卡了好久。。。逐行检查结果发现是sort函数写错了!!
    我的区间是1到n的。应该是sort(lr+1, lr+n+1);好气啊。。。

  • 代码
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;



struct T {
    double l, r;
};

int cs;
int n;
double d;
T lr[2016];


bool cmp1(T t1, T t2) {
    return t1.l < t2.l;
}

bool cmp2(T t1, T t2) {
    return t1.r < t2.r;
}
double jl(double x, double x1, double y1) {
    double t;
    t = sqrt(y1*y1 + (x-x1)*(x-x1) );
    return t;
}

int main() {
    cin >> n >> d;
    cs = 0;
    while (!(n==0 && d==0)) {
        bool ok = 1;
        for (int i = 1; i <= n; i++) {
            double x, y;
            cin >> x >> y;
            lr[i].l = x * 1.0 - sqrt(d*d - y * y);
            lr[i].r = x * 1.0 + sqrt(d*d - y * y);
            if (fabs(y) > d) ok = 0;
        }

        if (n <= 0 || d <= 0) ok = 0;

        if (!ok) {
            printf("Case %d: %d\n", ++cs, -1);
        }
        else {
            int ans = 0;
//          sort(lr+1, lr+n+1, cmp1);
            sort(lr+1, lr+n+1, cmp2);


            int now = 1;
            while (now <= n) {
                ans++;
                double nl;
                nl = lr[now].r;
                now++;
                while (nl >= lr[now].l && now <= n) now++;
            }

//          T temp = lr[1];
//          ans = 1;
//          for (int i = 2; i <= n; i++)
//          {
//              if (lr[i].l > temp.r)
//              {
//                  ans++;
//                  temp = lr[i];
//              }
//              else if (lr[i].r < temp.r)
//              {
//                  temp = lr[i];
//              }
//          }
            cout << "Case " << ++cs << ": " << ans << endl;
        }




        cin >> n >> d;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值