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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章