您的位置:首页 > 其它

POJ1328——Radar Installation

2014-06-01 09:36 351 查看
Radar InstallationDescriptionAssume 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.InputThe 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
OutputFor 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轴上可以放置雷达(放置位置可为小数),以放置位置为圆心,d为半径做圆来覆盖岛屿,输出最小的雷达数以覆盖所有的岛屿。无法覆盖输出-1。
解题思路:假设某个岛屿的坐标为(x,y),则在(x-sqrt(d*d-y*y),x+sqrt(d*d-y*y))范围内的雷达可以覆盖到此岛屿。
     先求出各个岛屿的可覆盖雷达范围,在根据其区间左端点进行排序,再通过贪心确定需要的最少雷达数。
     ps:当一个岛屿的纵坐标大于d时,不可能覆盖到,输出-1。
     ps2:进行贪心时,设置一个变量tmpr=p[1].r。
当p[i].r<tmpr时,tmpr=p[i].r 否则p[i]点会漏掉。
当p[i].l>tmpr是,tmpr=p[i].r,cnt++
Code:
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct point
{
double x,y;
double l,r;
} p[100000];
bool cmp(struct point a,struct point b)
{
return a.l<b.l;
}
int main()
{
double m,d,tmpr;
int i,n,ok,cnt,times=0;
while (scanf("%d %lf",&n,&d)!=EOF)
{
times++;
ok=1;
if (n==0&&d==0) break;
for (i=1; i<=n; i++)
{
scanf("%lf %lf",&p[i].x,&p[i].y);
if (p[i].y>d) ok=0;
p[i].l=p[i].x-sqrt(d*d-p[i].y*p[i].y);
p[i].r=p[i].x+sqrt(d*d-p[i].y*p[i].y);
}
if (ok)
{
sort(p+1,p+n+1,cmp);
tmpr=p[1].r;
cnt=1;
for (i=2; i<=n; i++)
{
if (p[i].l>tmpr) tmpr=p[i].r,cnt++;
if (p[i].r<tmpr) tmpr=p[i].r;
}
printf("Case %d: %d\n",times,cnt);
}
else printf("Case %d: -1\n",times);
}
return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: