Radar Installation
Radar Installation
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/C
题目:
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<tex2html_verbatim_mark> distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d<tex2html_verbatim_mark> .
We use Cartesian coordinate system, defining the coasting is the x<tex2html_verbatim_mark> -axis. The sea side is above x<tex2html_verbatim_mark> -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<tex2html_verbatim_mark> - y<tex2html_verbatim_mark>coordinates.
Input
The input consists of several test cases. The first line of each case contains two integers n<tex2html_verbatim_mark>(1n1000)<tex2html_verbatim_mark> and d<tex2html_verbatim_mark> , where n<tex2html_verbatim_mark> is the number of islands in the sea and d<tex2html_verbatim_mark> is the distance of coverage of the radar installation. This is followed by n<tex2html_verbatim_mark> 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
题意:
给出n个岛屿的坐标,和雷达可以探测的半径。要求用最少的雷达把所有岛屿都探测到(雷达必须放在x轴上),
输出所用雷达的个数。
分析:
将能探测到岛屿的雷达位置区间求出来,这样就将问题转化为了区间覆盖问题。
按照右交点排序,如果i点的左交点在当前雷达右交点的右边 则需安装一个新雷达,更新雷达。
如果不能覆盖记得输出‘-1’。
注意: 如果是按左交点排序,方法相同不过要考虑雷达的左右交点都在当前雷达右交点左边的情况。
1 #include<iostream> 2 #include<cmath> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int maxn=1005; 7 struct node 8 { 9 double l; 10 double r; 11 }a[maxn]; 12 double cmp(node a,node b) //进行右交点排序 13 { 14 if(a.r==b.r) a.l>b.l; 15 return a.r<b.r; 16 } 17 int main() 18 { 19 int n,d,i,j; 20 double x,y; 21 int c=0,cnt=1; 22 while(cin>>n>>d&&(n||d)) 23 { 24 c++; 25 cnt=1; 26 for( i=0;i<n;i++) 27 { 28 cin>>x>>y; 29 if(y>d||d<0) 30 { 31 cnt=-1; 32 continue;} 33 a[i].l=x-sqrt(d*d-y*y); //求出能探测岛屿最左边雷达的坐标 34 a[i].r=x+sqrt(d*d-y*y); //最右边的坐标 35 } 36 sort(a,a+n,cmp); 37 for(i=1,j=0;i<n;i++) 38 { 39 if(cnt==-1) break; 40 if(a[j].r<a[i].l) 41 { 42 cnt++; 43 j=i; 44 } 45 } 46 cout<<"Case "<<c<<":"<<' '<<cnt<<endl; 47 48 } 49 return 0; 50 }