Radar Installation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 67532 | Accepted: 15147 |
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
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
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
题意是在平面上有一些卫星,要在X轴上建立一些雷达,雷达有相同的辐射半径,问想要把所有卫星都辐射到需要的最少的雷达数量。如果不可能都辐射到的话,输出-1。
2012年做的题,很经典的贪心。实质上这个问题贪的是区间,要找到最大数量重合的区间,然后再找下一个最大数量重合的区间,这时结果就+1。
代码:
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
struct ss
{
double x,y;
}a[1005];
int cmp(ss x,ss y)
{
if(x.x==y.x)
return x.y<y.y;
else return x.x<y.x;
}
int main()
{
int island,count,result=0,flag=0,R,island_x[1005],island_y[1005];
double radar;
cin>>island>>R;
while(island!=0||R!=0)
{
for(count=1;count<=island;count++)
{
cin>>island_x[count]>>island_y[count];
if(island_y[count]>R||island_y[count]<0||R<=0)
{
result=-1;
}
else
{
a[count].x=island_x[count]-sqrt(1.0*(R*R-island_y[count]*island_y[count]));
a[count].y=island_x[count]+sqrt(1.0*(R*R-island_y[count]*island_y[count]));
}
}
if(result!=-1)
{
sort(a+1,a+island+1,cmp);
radar=a[1].y;result=1;
for(count=2;count<=island;count++)
{
if(a[count].x>radar)//当前区间不能满足重合的要求了,要增加一个区间了
{
result++;
radar=a[count].y;
}
else if(a[count].y<radar)
{
radar=a[count].y;//缩小重合区间
}
}
cout<<"Case "<<++flag<<": "<<result<<endl;
result=0;
}
else
{
cout<<"Case "<<++flag<<": "<<result<<endl;
result=0;
}
cin>>island>>R;
}
return 0;
}