1. 程式人生 > >Radar Installation

Radar Installation

namespace total col present accept 是否 assume 站點 div

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;
    }    
}

Radar Installation