POJ 1328.Radar Installation

题目链接:http://poj.org/problem?id=1328

题目: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
解题思路:每个岛i要被雷达覆盖,对雷达的安置地点有一个要求,即雷达安置的位置应该是在x轴上的一个区间[ibegin,iend]。如果存在岛j的覆盖区间[jbegin,jend]属于[ibegin,iend],那么覆盖岛j的雷达也一定能覆盖到i。因此将区间按ibegin升序排序,删掉存在i,j满足[jbegin,jend]属于[ibegin,iend]的所有区间i。那么剩下的区间就不存在包含关系。如果下一个区间的开始大于上一个区间的结束即j=i+1是jbegin>iend则需要增加一个雷达。实现代码如下:
#include <cstdio>
#include <cmath>
#include <algorithm>

using namespace std;

struct s_region
{
	double beg, end;
}data[1000];

double x[1000], y[1000];

bool visited[1000]; 

bool cmp (s_region a, s_region b)
{
	return a.beg < b.beg;
}

int main()
{
	int numIsland;
	double dist;
	int casei = 0;
	while (scanf ("%d%lf", &numIsland, &dist) != EOF)
	{
		if (numIsland == 0 && dist == 0.0)
			break;
		for (int i = 0; i < numIsland; ++i)
		{
			scanf ("%lf%lf", &x[i], &y[i]);  
		}

		printf ("Case %d: ", ++casei);

		int flag = false;
		if (dist == 0.0)
		{
			flag == true;
		}
		else
		{
			for (int i = 0; i < numIsland; ++i)
			{
				if (y[i] > dist)
				{
					flag = true;
					break;
				}
				data[i].beg = x[i]-sqrt(dist*dist-y[i]*y[i]);  
				data[i].end = x[i]+sqrt(dist*dist-y[i]*y[i]);
			}
		}

		if (flag)
		{
			printf ("-1\n");
			continue;
		}

		sort (data, data + numIsland, cmp);

		memset(visited, 0, sizeof (visited));

		//若[jbeg,jend]属于[ibeg,iend]则将visited[i]标记为1
		for (int i = 0; i < numIsland; ++i)
		{
			for (int j = i + 1; j < numIsland; ++j)
			{
				if (data[j].beg > data[i].end)
					break;
				if (data[j].end <= data[i].end && data[j].beg <= data[i].end)
				{
					visited[i] = true;
					break;
				}
			}
		}

		int i = 0;
		while (i < numIsland && visited[i])
		{
			i++;
		}

		double end = data[i].end;
		int ans = 1;
		for (i = i + 1; i < numIsland; ++i)
		{
			if (visited[i] || data[i].beg <= end)
			{
				continue;
			}
			ans += 1;
			end = data[i].end;
		}

		printf ("%d\n", ans);
	}
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值