POJ-1328 区间贪心,几何

题目大意:以x轴为分界,y>0部分为海,y<0部分为陆地,给出一些岛屿坐标(在海中),再给出雷达可达到范围,雷达只可以安在陆地上,问最少多少雷达可以覆盖所以岛屿。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int n, m;
struct node
{
	double a;
	double b;
}e[1005];
bool cmp(node x, node y)
{
	return x.a < y.a;
}
int main()
{
	int c = 1;
	while (cin >> n >> m,n,m)
	{
		double a, b;
		int counting = 1;
		for (int i = 0; i < n; i++)
		{

			cin >> a >> b;
			if (b > m)
			{
				counting = -1;
			}
			double t = sqrt(m*m - b * b);
			e[i].a = a - t;
			e[i].b = a + t;

		}
		if (counting != -1)
		{
			sort(e, e + n, cmp);
			double s = e[0].b;
			for (int i = 1; i < n; i++)
			{
				if (e[i].a > s)
				{
					counting++;
					s = e[i].b;
				}
				else if (e[i].b < s)
				{
					s = e[i].b;
				}
			}
		}
		printf("Case %d: %d\n",c++, counting);
	}
}

猜你喜欢

转载自blog.csdn.net/yihanyifan/article/details/80181119