您的位置:首页 > 其它

贪心 POJ 1328

2016-08-02 23:34 274 查看
题意:假设海岸线是一条无限延伸的直线。每一个小的岛屿是海洋上的一个点。雷达坐落于海岸线上,只能覆盖d距离,所以如果小岛能够被覆盖到的话,它们之间的距离最多为d。

题目要求计算出能够覆盖给出的所有岛屿的最少雷达数目。



一目了然,求投影到X轴上,然后重叠位置只记一次,有两种情况:当此时的线段左端点大于此时比较的右端点,不会有重合了,更新;当当此时的线段右端点小于此时比较的右端点,范围更小了,更新比较线段(这样做都是建立在sort之后,每一线段的左端点不减)

#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <math.h>

using namespace std;

struct point
{
double left, right;
}p[2010], temp;

bool operator < (point a, point b)
{
return a.left < b.left;
}

int main()
{
int n;
double r;
int kase = 0;
while (cin >> n >> r && (n || r))
{
bool flag = false;
for (int i = 0; i < n; i++)
{
double a, b;
cin >> a >> b;
if (fabs(b) > r)
{
flag = true;
}
else
{
p[i].left = a * 1.0 - sqrt(r * r - b * b);
p[i].right = a * 1.0 + sqrt(r * r - b * b);
}
}
cout << "Case " << ++kase << ": ";
if (flag)
{
cout << -1 << endl;
}
else
{
int countt = 1;
sort(p, p + n);
temp = p[0];

for (int i = 1; i < n; i++)
{
if (p[i].left > temp.right)
{
countt++;
temp = p[i];
}
else if (p[i].right < temp.right)
{
temp = p[i];
}
}
cout << countt << endl;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: