Radar Installation

Radar Installation
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 562, Accepted users: 500
Problem 10023 : No special judgement
Problem 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. 

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
Problem Source
Bei jing 2002

解题思路:

radar installation
在海里有很多小岛,每个小岛位置由x,y坐标确定。而我们只能在海岸线上建雷达站,同时给定雷达站的最大覆盖距离d(以雷达站为圆心,d为半径),要求所有小岛都要被雷达站覆盖,求最少的雷达站数目,如果没有解决方案,输出-1。

 

贪心策略1:让某个雷达站覆盖尽量多的小岛?--》不正确
贪心策略2:从左往右建雷达站:最左边的雷达站需要覆盖最左边的小岛,同时位置尽量右靠。依次重复。
1. 如何确定雷达可能位置:以小岛为圆心,画圆,与海岸线的右交点为建雷达站点。
2. 对于所有小岛,求出左右交点,从左至右,以第一个右交点为基准,
如果下一海岛的左交点小于或等于该右交点,则此海岛能够被该雷达站覆盖,
不过这里要注意,如果出现该海岛的右交点小于或该右交点的情况,基准应该换成该海岛的右交点;
如果下一海岛的左交点大于该右交点,则需要新建雷达站,雷达站数++,同时以这一海岛的右交点为基准,重复上步骤;

 

代码(C++):

#include<iostream>
#include <algorithm>
#include<cmath>
using namespace std;

struct node{
    double left,right;//以每个岛为圆心,与x轴的左右交点 
}island[1001];//1001个实例 

//sort的比较函数 
bool cmp(node a,node b){
    return a.left<b.left;
}

int main(){
    int n=0;double d;//n为小岛个数,d为雷达能探测的最大距离 
    int CaseNum=0;//第CaseNum个测试用例 
    int flag=0;//flag为是否有解决方案的标志,0为有,1为无 
    double x,y;//x,y为小岛的左右坐标 
    while(cin>>n>>d && n!=0){
        CaseNum++; 
        flag=0; 
        
        //输入,并记录island.left 和island.right 
        for(int i=0;i<n;i++){
            cin>>x>>y;
            //如果小岛到x轴的距离y大于d,则没有解决方案
            if(y>d) {
                flag=1;//没有解决方案 
            }
            //记录以每个小岛为圆心,d为半径的圆与x轴的左右交点 
            island[i].left=x-sqrt(d*d-y*y);
            island[i].right=x+sqrt(d*d-y*y);
        }
        
        //没有解决方案 
        if(flag==1) {
            cout<<"Case "<<CaseNum<<": -1"<<endl;
            continue;
        }
        
        //否则: 
        //排序 , 按x轴的左交点从左到右排序 
        sort(island,island+n,cmp);
         
       //循环,贪心,从最左边的圆开始
         int count=1;
         int tmp=island[0].right;
         for(int i=0;i<n;i++){
             if(island[i].left>tmp){ 
                 count++;
                 tmp=island[i].right;
             }
             else if(island[i].right<tmp){
                 tmp=island[i].right;
             }
         }
         
         //输出 
        cout<<"Case "<<CaseNum<<": "<<count<<endl;
    }    
}

 

posted @ 2017-11-29 16:44  Hazel_97  阅读(173)  评论(0编辑  收藏  举报