nyoj 题目287 Radar 贪心算法

这篇博客讨论了一种贪心算法的应用,即如何在海岸线上布置最少数量的雷达站,以便覆盖所有位于海上的岛屿。题目设定雷达站的覆盖范围为d,每个岛屿的坐标用(x, y)表示,y轴上方为海,下方为陆地。通过计算每个岛屿的覆盖区间,并进行排序,可以找到能够覆盖所有岛屿的最小雷达站数量。博客提供了算法实现和样例输入输出,展示了如何处理这种情况下的区间覆盖问题。
摘要由CSDN通过智能技术生成

Radar

时间限制:1000 ms  |  内存限制:65535 KB难度:3

描述

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.

 转存失败重新上传取消

输入

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

输出

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.

样例输入

3 2
1 2
-3 1
2 1

1 2
0 2

0 0

样例输出

Case 1: 2
Case 2: 1

来源

Beijing 2002

上传者

ctest 大意: 海上有n个海岛,图中y上半轴为海,下半轴为陆地。现布置一些雷达在x轴上,作用半径为d,求覆盖全部海岛的最少雷达数。输入第一行为n,d;以后的n行为n个海岛的坐标x,y;以0,0代表结束,如果不能完全覆盖,输出-1. 思路: 如果雷达可以覆盖到海岛,那么以海岛为圆心,以d为半径画圆在x轴必相交(最坏也是相切),那么就有一段区间,在这个区间内雷达在哪都能覆盖这个海岛,如果两个海岛作圆,如果区间有重叠,在重叠处放置一个雷达就可覆盖两个海岛,以此类推。那么就转化成了贪心算法中区间找点的问题,将每个海岛在x轴形成的坐标区间,保存到数组,并排序,再判断。

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct fun
{
double top;//左坐标 
   double last;//右坐标 
}s[1000];
int  cmp(const void*a,const void*b)
{
return (*(fun*)a).last>(*(fun*)b).last?1:-1;
}
int main()
{
int k=0;
double n,r;
while(~scanf("%lf %lf",&n,&r))
{
int flag=1;
if(n==0&&r==0) break;
double x,y;
for(int i=0;i<n;i++)
{
scanf("%lf %lf",&x,&y);
if(y>r)//海岛距离太远,雷达半径不够,不能覆盖 
{
flag=0;
break;
}
s[i].top = x-sqrt(r*r-y*y); 
s[i].last = x+sqrt(r*r-y*y);
}
qsort(s,n,sizeof(s[0]),cmp);//以右坐标从小到大排序 
int sum=1;
double temp=s[0].last ;//左边第一个肯定放置一个,
 for(int i=1;i<n;i++)//从第二个开始判断是否有重叠
{
if(s[i].top > temp)//左坐标大于上一个右坐标 ,不重叠,要放置雷达 
{
sum++;
temp=s[i].last ;
}

}
if(flag==1)
printf("Case %d: %d\n",++k,sum);
else printf("-1\n");
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

G11176593

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值