Radar Installation POJ - 1328 贪心(区间覆盖)

传送门


题目大意:海上有n个小岛(的坐标), 海岸线(x轴)上安装雷达, 使雷达能够覆盖所有的小岛,然后给出雷达覆盖半径,求出最少安装几个雷达能覆盖所有的小岛。


解题思路:一看很明确就是用区间覆盖的贪心, 但是由于是圆,不好算,但是所有雷达的纵坐标是确定的, 因此我们把问题能转换成x左边的情况,也就是区间覆盖了。  要想覆盖这个岛, 设小岛坐标为(x, y),则能覆盖它的圆心必须位于[x-sqrt(d*d - y*y),   x+sqrt(d*d - y*y) ]  内


代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>


using namespace std;
int n, d;

struct Point
{
	double l, r;
	bool operator<(const Point & a) const
	{
		return this->l < a.l;
	}
}P[1005];

int main()
{
	int Case = 0;
	while(scanf("%d%d", &n, &d) && n+d)
	{
		int ok = 1;
		int x, y;
		for(int i=0; i<n; ++i)
		{
			scanf("%d%d", &x, &y);
			if(y > d) ok = 0;
			if(!ok) continue;
			P[i].l = (double)x - sqrt((double)d*d - y*y);
			P[i].r = (double)x + sqrt((double)d*d - y*y);
		}
		if(!ok)
		{
			printf("Case %d: -1\n", ++Case);
			continue;
		}

		sort(P, P+n);
		double now = P[0].r;
		int	cnt = 1;
		for(int i=0; i<n; ++i)
		{
			if(P[i].r < now) now = P[i].r;
			if(P[i].l > now)
			{
				++cnt;
				now = P[i].r;
			}
		}
		printf("Case %d: %d\n", ++Case, cnt);

	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值