[挑战程序设计竞赛] POJ 1328 - Radar Installation

题意:

给定很多个岛屿的坐标,然后在X轴上安装雷达,问把覆盖到所有岛屿最少需要几个雷达。不能覆盖到所有岛屿则输出“-1”。

根据题意,可以根据勾股定理将每个岛屿的坐标转化成在X轴安装雷达的区间。如果有一个岛屿的纵坐标 > d 则一定输出“-1”,否则就有解。

直接把转化完的区间按区间结束位置从小到大排序。随后贪心即可解决。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <time.h>

typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;

struct ak
{
    double x, y;
}s[25001];
int cmp(const void *a, const void *b)
{
    struct ak c = *(struct ak *)a;
    struct ak d = *(struct ak *)b;
    return c.y > d.y ? 1 : -1;
}

int main()
{
    #ifdef _Test
        freopen("test0.in", "r", stdin);
        freopen("test0.out", "w", stdout);
        srand(time(NULL));
    #endif
    int n, d, ans, flag, cnt = 1;
    double r;
    while(~scanf("%d %d", &n, &d), n != 0 || d != 0)
    {
        flag = 1;
        int x, y;
        for(int i = 0; i < n; i++)
        {
            scanf("%d %d", &x, &y);
            if(y > d)
            {
                flag = 0;
            }
            if(!flag) continue;
            double dif = sqrt(d*d - y*y);
            s[i].x = x - dif;
            s[i].y = x + dif;
        }
        printf("Case %d: ", cnt++);
        if(flag)
        {
            qsort(s, n, sizeof(s[0]), cmp);
            r = s[0].y;
            ans = 1;
            for(int i = 1; i < n; i++)
            {
                if(s[i].x > r)
                {
                    r = s[i].y;
                    ++ans;
                }
            }
            printf("%d\n", ans);
        }
        else
        {
            printf("-1\n");
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值