POJ1328-Radar Installation

15 篇文章 0 订阅

一开始以为将各个小岛的坐标排序从最左端开始找到使该小岛在雷达边缘的雷达坐标进行贪心即可。

事实证明:我还是太年轻......

正确做法是将每个小岛坐标求出圆心所在的区间,区间重合的地方就是雷达的设置点。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <utility>
#include <algorithm>

using namespace std;

const int MAXN = 1000;
typedef pair<double, double> P;
P coor[MAXN+2];

int main()
{
    int n, d;
    int ser = 1;
    
    while (scanf("%d%d", &n, &d) == 2) {
        if (n == 0 && d == 0) {
            break;
        }
        int i;
        bool flag = true;
        for (i = 0; i < n; i++) {
            scanf("%lf%lf", &coor[i].first, &coor[i].second);
            if (coor[i].second > d) {
                flag = false;
            }
            else {
                double temp = coor[i].second;
                coor[i].second = coor[i].first + sqrt(pow(d, 2) - pow(temp, 2));
                coor[i].first = coor[i].first - sqrt(pow(d, 2) - pow(temp, 2));
            }
        }
        
        int cnt = 0;
        if (flag == false) {
            cnt = -1;
        }
        else {
            sort(coor, coor + n);
            
            i = 0;
            double curx = coor[i].second;
            while (i < n) {
                while (i < n && coor[i].first <= curx) {
                    curx = min(curx, coor[i++].second);
                }
                curx = coor[i].second;
                cnt++;
            }
        }
        printf("Case %d: %d\n",ser++, cnt);
    }
    
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值