POJ 1328

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

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
#define ll long long int 
#define ull unsigned long long int 
#pragma warning(disable:4996)
#define pf printf
#define sf scanf
#define min(a,b) (a)>(b)?(a):(b);
double PI = acos(-1.0);


/*
以每个岛屿为圆心,d为半径画圆,与x轴产生两个交点,再将这些点按与x轴右交点从小到大排序
其后的点左交点再此有交点左边的可以被覆盖,之后再将这个点移到第一个不能被覆盖的点上,重复此过程。
*/
struct isl {
    double sta, end;
};


bool cmp(isl a, isl b) {
    return a.end < b.end;
}


int main(void) {
    isl is[1005];//之前开小了,注意题目是1000
    int n,d;
    int x, y;
    int p = 1;
    while (cin >> n >> d, n + d) {
        int maxy = 0;
        int ans = 1;
        for (int i = 0; i < n; i++) {
            cin >> x >> y;
            if (maxy < y)
                maxy = y;
            is[i].sta = x - sqrt(d*d - y * y);
            is[i].end = x + sqrt(d*d - y * y);

        }
        getchar();
        getchar();

        if (maxy > d) {//PE了很多次
            cout << "Case " << p++ << ": " << -1 << endl;
            continue;
        }

        sort(is, is + n, cmp);
        double vis = is[0].end;//从第一个有交点开始
        for (int j = 0; j < n; j++) {
            if (is[j].sta <= vis)  continue;//可以被覆盖

            else//不能被覆盖,移动vis到不能覆盖的第一个点
                ans++, vis = is[j].end;
        }

        pf("Case %d: %d\n", p++, ans);
    }
    return  0;
}

猜你喜欢

转载自blog.csdn.net/jiruqianlong123/article/details/81299119
今日推荐