欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ - 1328 Radar Installation 【贪心】

程序员文章站 2022-03-26 14:07:03
...

Radar Installation

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 107163   Accepted: 23805

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.

                                                       POJ - 1328 Radar Installation 【贪心】
                                                               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

题意:

在海中有许多的小岛,现在要在笔直的海岸线上布置雷达,雷达的覆盖半径是d,问最少需要多少个雷达才可以将全部的小岛都覆盖到,如果不能都覆盖到输出-1。

思路:

首先不可能的情况只有小岛离海岸线的垂直距离大于d 。

在确定有解后我们考虑,  一个雷达覆盖的位置,当然让它尽量覆盖的越多越好,尽量使雷达最大的发挥他的作用,那么雷达的位置应该是这种情况:

                       POJ - 1328 Radar Installation 【贪心】

四角星为海岛位置,圆为雷达覆盖的地方,最大的利用雷达,那么我们可以使得雷达在覆盖到当前没有覆盖到的最左边的海岛的情况下尽量往右覆盖,注意我这里的没有覆盖到的最左边的海岛指的是如图:

POJ - 1328 Radar Installation 【贪心】

明显a1从位置上来说在a2的左边,但是如果让雷达刚好覆盖到a1需要将雷达布置在R1,让雷达刚好覆盖到a2需要将雷达布置在R2,但R2在R1的左边,所以我们可以说“ a2在a1的左边 ”,所以将每个海岛 ai 对应的圆心 Ri 找出来,排序贪心的选取就好了,圆心的位置勾股一下很容易求出来。

代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long Lint;
const int max_n=1e3+100;

struct Node{
    int x;
    int y;
    long double xx; //xx即圆心位置
}node[max_n];
bool cmp(Node n1,Node n2){
    return n1.xx<n2.xx;
}
int n;
long double d;
void solve(){
    int cas=1;
    int dd;
    while(scanf("%d%d",&n,&dd) && !(n==0&&dd==0)){
        d=dd;
        bool f=true;
        for(int i=0;i<n;i++){
            scanf("%d%d",&node[i].x,&node[i].y);
            if(node[i].y>d) f=false;
            node[i].xx=sqrt(d*d-node[i].y*node[i].y)+node[i].x;
        }
        sort(node,node+n,cmp);
        int res=0;
        for(int i=0;i<n;i++){
            long double index=node[i++].xx;
            while(i<n && ((index-node[i].x)*(index-node[i].x)+node[i].y*node[i].y)<=d*d ){
                i++;  //把与当前选择的圆心距离小于d的都包含进来
            }
            i--;
            res++;
        }
        if(!f) res=-1;
        printf("Case %d: %d\n",cas++,res);
    }
}

int main(){
    solve();
}