Radar Installation -区间问题

Radar Installation
在这里插入图片描述

题目

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
假设滑行是一条无限长的直线。陆地在海岸的一边,海洋在另一边。每个小岛都是位于海边的一个点。而任何位于海岸线上的雷达装置,只能覆盖d个距离,因此,如果它们之间的距离不超过d,海上的一个岛屿就可以被半径装置覆盖。

我们使用笛卡尔座标系,定义滑行是x轴。海侧高于x轴,陆侧低于x轴。给定海中每个岛屿的位置,以及雷达装置覆盖范围的距离,您的任务是编写一个程序,找出覆盖所有岛屿的雷达装置的最小数量。请注意,岛的位置由其x-y座标表示。

图A雷达装置的样本输入

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
输入由几个测试用例组成。每种情况的第一行包含两个整数n(1<=n<=1000)和d,其中n是海里的岛屿数,d是雷达装置的覆盖距离。接下来是n行,每行包含两个整数,表示每个岛位置的座标。接下来是一个空行来分隔这些案例。

输入端由包含一对零的行终止

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.
对于每个测试用例输出,一行由测试用例编号和所需的最少雷达安装数组成。”-1“安装意味着没有解决方案。

Sample Input

3 2
1 2
-3 1
2 1

1 2
0 2

0 0

Sample Output

Case 1: 2
Case 2: 1

题解

题解
把这个问题转换成区间问题
寻找有多少个没有交集的区间
如果有交集 则在第一个区间的最右添加雷达
根据我上面的图可以知道,最右添加是可以的
如果是有区间在第一个区间的里面
则在这个区间的最右
// #include <iostream>
#include <algorithm>
#include <vector>
#include <stdio.h>
#include <cmath>
using namespace std;
struct pair1
{
    double first, second;
} p[1010];
bool cmp(pair1 e1, pair1 e2)
{
    return e1.first < e2.first;
}
int main()
{
    int N, R;
    int cnt = 1;
    while (~scanf("%d %d", &N, &R))
    {
        if (N == 0 && R == 0)
            break;
        int ans = 1;
        for (int i = 0; i < N; i++)
        {
            double x, y;
            scanf("%lf %lf", &x, &y);
            // d是x 距离⚪在x轴上的交点的距离
            double d = sqrt(R * R - y * y);
            // 求出与x轴的两个交点
            // 现在就转换成了区间问题
            p[i].first = x - d;
            p[i].second = x + d;
            // 如果范围不够
            if (y > R || y < 0 || R <= 0)
                ans = -1;
        }
        // 转换成区间问题

        sort(p, p + N, cmp);
        // 第一个是要按装的
        double t = p[0].second;
        // 如果两个区间有交集 那么就可以覆盖的住
        for (int i = 1; i < N && ans != -1; i++)
        {
            // 两个区间没有交集
            if (p[i].first > t)
            {
                ans++;
                t = p[i].second;
            }
            if (p[i].second < t)
            {
                t = p[i].second;
            }
        }
        printf("Case %d: %d\n", cnt++, ans);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章