Radar Installation
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 96398 Accepted: 21418
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
题意:给你N个点求,用半径为d的圆去覆盖所有的点,圆的圆心只能在x轴。
很巧妙的一道题,读入时首先判断是否有点的y > d,这样就无法用圆来覆盖,因为圆的坐标只能在x轴上,所以y = 0,且d是固定的,所以对于每个点,能覆盖该点的范围是确定的,可以(x-a)^2 + y^2 < d^2,可以求出a的范围,就能覆盖该点的圆的x坐标范围。然后题目就变成了点覆盖区间的问题了,要保证每个区间都有一个点即可,只要把范围second从小到大,second相等的按first从小到大排序,然后再从头到尾扫描一遍即可。如果碰到无法覆盖的就更新end
代码如下:
#include<iostream>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
typedef pair<double,double> P;
const int MAX = 1010;
const double eps = 1e-6;
P p[MAX];
int N,D;
double dis;
void add(double u,double v,int i){//求出每个点,能覆盖该点的圆的x坐标
double x = sqrt(dis - v*v);
p[i].first = -x + u;
p[i].second = x + u;
}
bool cmp(P A,P B){//排序,注意要先按second排序
if(abs(A.second - B.second) < eps)
return A.first < B.first;
else
return A.second < B.second;
}
int solve(){
sort(p+1,p+1+N,cmp);
int res = 1;
double end = p[1].second;
for(int i=2;i<=N;++i){
if(p[i].first > end){//更新end
res++;
end = p[i].second;
}
}
return res;
}
int main(void){
int Case = 0;
while(cin >> N >> D){
dis = D*D;
if(N == 0 && abs(D - 0) < eps){
break;
}
double x,y;
bool isok = true;
for(int i=1;i<=N;++i){
cin >> x >> y;
if(y - D > eps){
isok = false;
}
else{
add(x,y,i);
}
}
cout << "Case " << ++Case << ": ";
if(!isok){
cout << -1 << endl;
}
else{
cout << solve() << endl;
}
}
return 0;
}