poj1328雷达安装【贪心法】

问题简述:x轴上方有很多岛屿,在x轴上放雷达,用最少的雷达使得所有岛能够被覆盖到

原问题:

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. 

poj1328雷达安装【贪心法】 
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为半径画一个圆,该圆与x轴相交的那一段就是我们要求的区间),暂且把这个区间叫做覆盖区间。

从最左边的点(岛屿)开始看,观察它的覆盖区间,如果要放一个雷达,直观上我们更希望放到区间的最右端,这样能覆盖更多右边剩下的点;

看下一个点,如果它的覆盖区间与上一个点的区间有重叠,就说明如果把雷达放在那个重叠的部分时就可以同时覆盖这两个点。

再看第三个点,如果第三个点的覆盖区间也与前两个点有重叠,那么要看是不是三个点都重叠在一起;如果不是,那这第三个点其实并不能与前两个点公共用一个雷达;如果三个区间有一块确实重叠在一起了,那说明三个点可以共用同一个雷达。

因此,我们判断是不是要再加一个雷达的关键,就是那个“第一个点”的区间与后续几个点的区间有无重叠,第一个点的最右如果大于其余点的最左,就不用加雷达。反之,如果出现了一个点,它的最左大于“第一个点”的最右了,就说明不能共用了,要再加一个雷达,而后面的点的最左就要跟这个新的“第一个点”来比较了。

所以我们可以设一个变量来记录当前的“第一个点”的最右侧,遍历点集更新这个值,这个值每变一次就说明要加一个雷达。

 

代码:

#include <iostream>

#include <cmath>

#include <algorithm>

using namespace std;

 

int n,d;//n是点数,d是雷达覆盖范围半径

 

struct point

{

    double left;

    double right;

}range[20001];//存放每个点的覆盖范围

 

int cmpr(point a,point b)//排序规则(按每个点的覆盖范围最右端来排序)

{

    return a.right < b.right;

}

 

int main()

{

    int k = 1;

    while (cin >> n >> d && n != 0)

    {

        int ans = 1;//需要几个雷达

        bool able = 1;//无解判断,如果无解则able = 0,不再参与后续运算

        if ( d < 0 )

            able = 0;

        for (int i = 0; i < n; i ++)

        {

            double x,y;

            cin >> x >> y;

            if (y > d || y < 0)

                able = 0;

            range[i].left = x - sqrt(d*d - pow(y,2));//可以装雷达的范围(左侧)

            range[i].right = x + sqrt(d*d - pow(y,2));//可以装雷达的范围(右侧)

        }

        if (able != 0)

        {

            sort(range,range+n,cmpr);

            

            double rangeMAXr = range[0].right;//当前能覆盖到的最大范围(右侧)

            for (int i = 0; i < n; i++)//扫一遍

            {

                if (range[i].left > rangeMAXr)//如果这个点超过了当前能覆盖的最大范围(不能被覆盖到),就要加一个雷达

                {

                    rangeMAXr = range[i].right;

                    ans++;

                }

            }

        }

        if (able == 0)

            cout << "Case " << k << ": " << -1 << endl;

        else

            cout << "Case " << k << ": " << ans << endl;

        k++;

    }

}

上一篇:dede数据库类使用方法 $dsql


下一篇:adodb.RecordSet的属性和方法