POJ1328-Radar Installation(贪心)

POJ1328

Radar Installation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 93037 Accepted: 20765

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

Source



题目大意:

一个二维坐标系,y>0表示为海y<0表示在岸上,给你几个岛的坐标(一定在海上)和雷达的半径,问你最少用几个雷达可以将岛全部侦测到(雷达只能在岸上)

样例分析:

3 2(3个岛,雷达的半径为2)

1 2

-3 1

2 1   (三个岛的坐标)

输出

Case 1:2(最少需要两个雷达)


思路:

首先要求最少的雷达,那么雷达肯定是要放在x轴上的(毋庸置疑),只有这样雷达的范围才会最大,那么,我们是不是可以求出岛到x轴上每一点的距离d,对每一个点进行统计,统计这个点上d<=r的岛有多少个,再进行排序.....(这是错误的)根本不能这样做,先不谈x轴上的点有无穷多个,就算是有限的,这样做也难以实现.

那么该如何去做呢?单个点无法实现,那么我们是否可以求出以岛为圆心,雷达半径为半径的一个圆与x轴构成的一个区间,这个区间内的雷达,一定可以侦测到这个岛,而且在这个区间之外的雷达是肯定无法侦测到这个岛的


那么我们就可以记录每个岛的这个区间,按区间左端点进行排序之后遍历,如果区间重复那么合并代表这两个岛可以用一个雷达侦测,用一个R表示公共区间的右端点,我们只需要判断island[i].l与R的大小即可判断第i个岛与当前公共区间是否重复,若重复说明这个岛可以被这个公共区间的雷达侦测到,如果不重复就新放一个雷达;

PS:注意,如果区间重复而且第i个岛的右端点也被包含在公共区间内,那么公共区间的R要更新成第i个岛的r,否则第i+1个岛可能与第i个岛不重复(我这里wa了好久



代码如下

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef struct
{
    double x;
    int y;
    double l,r;
}ISLAND;
ISLAND island[1010];
int main()
{
    int m,d;
    int flag;
    int CASE=0;
    while((scanf("%d %d",&m,&d)!=EOF))
    {
        flag=0;
        CASE++;
        if(d<=0) flag=1;
        if(m==0&&d==0) break;
        for(int i=0;i<m;i++)
        {
            cin>>island[i].x>>island[i].y;  //岛的x,y坐标
            if(island[i].y>d) flag=1;
        }
        if(flag==1)
        {cout<<"Case "<<CASE<<": "<<"-1"<<endl;continue;}
        for(int i=0;i<m;i++)
        {
            double delta=sqrt(double(d*d-island[i].y*island[i].y));
            island[i].l=island[i].x-delta;                    //计算岛区间的l与r
            island[i].r=island[i].x+delta;
        }
        for(int i=0;i<m-1;i++)
        {
            for(int j=i+1;j<m;j++)
            {
                if(island[i].l>island[j].l)
                {
                    ISLAND t; 
                    t=island[i];
                    island[i]=island[j];       //按照l的大小排序
                    island[j]=t;
                }
            }
        }
        int ans=1;
        double R=island[0].r;    //R代表的是公共区间的右端点
        for(int i=1;i<m;i++)
        {
            if(island[i].l>R)      //第i个岛的左端点大于公共区间的右端点,即第i个岛的区间与当前公共区间不重复即公共区间的那个雷达不能侦测到第i个岛
            {
                ans++;          //因为不能侦测所以这里我们要放一个新的雷达,雷达数++
                R=island[i].r;      //舍弃掉之前的公共区间,新的公共区间的右端点为第i个岛的右端点r
            }
            else
            {
                if(island[i].r<R)
                {
                    R=island[i].r;
                }
            }
        }
        cout<<"Case "<<CASE<<": "<<ans<<endl;
    }
    return 0;
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值