您的位置:首页 > 其它

POJ1328 Radar Installation

2012-10-10 09:38 337 查看
  原题传送:http://poj.org/problem?id=1328

  贪心。

  每个岛屿在海岸线上都有一个雷达覆盖区间[x - sqrt(r2 - y2), x + sqrt(r2 - y2)],将这些区间按左区间从小到大排序,选择第一个区间,然后在后面找能够重叠的区间,如果能够重叠,那么更新该区间的右端点(取小的),遍历一遍就完事儿了。

  (ps:有时候调了精度反而会wa)

View Code

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#define N 1005

const double eps = 1e-8;

struct node
{
double x, y;
}e
;

struct nd
{
double s, t;
bool operator < (const nd &tmp) const
{
return s < tmp.s;
}
}a
;

int main()
{
double r;
int n, i, flag, cas = 1, cnt;
while(scanf("%d%lf", &n, &r))
{
if(n == 0 && r < eps)
break;
for(flag = i = 0; i < n; i ++)
{
scanf("%lf%lf", &e[i].x, &e[i].y);
if(e[i].y > r)
flag = 1;
}
if(flag == 1 || r < 0)
{
printf("Case %d: -1\n", cas ++);
continue;
}
for(i = 0; i < n; i ++)
{
a[i].s = e[i].x - sqrt(r * r - e[i].y * e[i].y);
a[i].t = e[i].x + sqrt(r * r - e[i].y * e[i].y);

}

std::sort(a, a + n);

double s = a[0].s;
double t = a[0].t;
for(cnt = i = 1; i < n; i ++)
{
if((a[i].s >= s && a[i].s <= t) || (a[i].t >= s && a[i].t <= t))  // 不调精度才AC
{
t = std::min(t, a[i].t);
}
else
{
cnt ++;
s = a[i].s;
t = a[i].t;
}
}
printf("Case %d: %d\n", cas ++, cnt);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: