您的位置:首页 > 其它

poj1328 radar installation

2016-03-17 16:27 225 查看


//以各岛为圆心做半径为b的圆,记录下每个圆与x坐标轴的左右交点,然后进行贪心选择。 本质上每一个圆的左右交点连成的线段就是一个雷达放置的位置范围
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
struct point{
double left;//左交点
double right;//右交点
}a[100],temp;
int cmp(point a,point b)
{
return a.left<b.left?1:0;//按照左交点的坐标从小到大排序
}
int main()
{
int n;
double r;
while(cin>>n>>r&&n&&r)
{
bool flag=false;
for(int i=0;i<n;i++)
{
double an,bn;//岛的坐标
cin>>an>>bn;
if(fabs(bn)>r)//说明无论雷达在哪里都不能覆盖岛
flag=true;
else
{
a[i].left = an * 1.0 - sqrt(r * r - bn * bn);
a[i].right = an * 1.0 + sqrt(r * r - bn * bn);
}
}

if (flag)
{
cout << -1 << endl;
}
else
{
int count = 1;
sort(a, a + n,cmp);  //按照左交点从小到大排序
temp = a[0];
for (int i = 1; i < n; i++)
{
if (a[i].left > temp.right)
{
count++;
temp = a[i];
}

}
cout << count << endl;
}
}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: