Radar Installation(POJ 1328)(贪心)

解决在海岸线上安装雷达以覆盖海上的多个岛屿的问题,通过排序和贪心算法找到最少的雷达数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://poj.org/problem?id=1328
Radar Installation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 80287 Accepted: 17933
Description

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d.

We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates.

Figure A Sample Input of Radar Installations

Input

The input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases.

The input is terminated by a line containing pair of zeros
Output

For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. “-1” installation means no solution for that case.
Sample Input

3 2
1 2
-3 1
2 1

1 2
0 2

0 0
Sample Output

Case 1: 2
Case 2: 1

题意:
一个海上有n个小岛,现在要在海岸线,也就是x轴上布置雷达,每个雷达的信号范围为一个以雷达位置为圆心半径为d的圆,问最少需要多少雷达,才能把所有的岛全都覆盖到。岛的位置按直角坐标表示。

题解:
用贪心的思想求解。看看每个岛被覆盖时相应的雷达范围是什么,然后把范围按照范围左端大小排序。这时,就先覆盖最左边的那个范围,依次往右边的范围走。
1、如果现在扫描到的范围的左端点,比现在雷达所能放置的最右位置要大,那么就说明要放另一个雷达,并把雷达放置的最右位置更新为当前范围的右端点。
2、如果现在扫描到的范围的右端点,比现在雷达所能放置的最右位置要小,那么就说明这个雷达的最右范围要以这个比较小的位置为准,雷达放置的最右位置就要更新为这个较小值。
思考:
1、千万不能在边输入边处理的时候,处理到某个条件就直接退出循环,这样会使后面的数据读不进来,程序出现问题。
2、可以逆向思维,不想雷达布置在什么位置覆盖了什么点,而是想岛能接收到信号的雷达位置范围,也就是以岛为圆心,d为半径做圆,与x轴交点之间的范围内布置雷达,就能把这个岛覆盖到。

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

using namespace std;

struct Point{
    double l, r;
};

const int maxn = 2010;
int n;
double d;
int minn;
Point p[maxn];

int Compare (Point a, Point b)
{
   return a.l < b.l;
}


int Calc(double x, double y, int i)
{
    if (d < fabs(y)) return 0;//要取绝对值
    double ans = sqrt (d * d - y * y);
    p[i].l = x * 1.0 - ans;
    p[i].r = x * 1.0 + ans;
    return 1;
}

void Count()
{
    sort (p, p + n, Compare);
    minn = 1;
    double Right = p[0].r;
    for (int i = 1; i < n; i++) {
        if (p[i].l > Right) {
            Right = p[i].r;
            minn++;
        } else if (p[i].r < Right) {//这样的话,肯定是包含关系,所以要把小的置为Right
            Right = p[i].r;
        }
    }
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen ("in.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    int kase = 1;
    while (scanf ("%d%lf", &n, &d) != EOF) {
        if (n == 0 && d == 0) break;
        printf ("Case %d: ", kase++);
        int flag = 0;//如果不能搭建,则flag为1
        for (int i = 0; i < n; i++) {
            double x, y;
            scanf ("%lf%lf", &x, &y);
            if (!Calc (x, y, i)) {//如果有一个岛无论怎样都没法被覆盖,则flag变为1
                flag = 1;//不能在这里退出出去,因为在边输入边处理,如果从这里退出去,则后面的无法被读取!!!
            }
        }

        if (flag) {
            printf ("-1\n");
            continue;
        }

        if (n == 0) {
            printf ("0\n");
            continue;
        }

        Count ();
        printf ("%d\n", minn);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值