POJ 1328 Radar Installation 区间贪心

来源:互联网 发布:php-> 编辑:程序博客网 时间:2024/06/15 10:36

对于每座岛屿,求出可能的雷达范围,也就是一个区间。(以岛屿为中心,d为半径画圆,与x轴的左交点就是区间的做断电,右交点就是右端点)

对于得到的区间按照x递增,x相同y递增的规则排序。

然后从前往后扫描,求出重叠区域,在重叠区域放置雷达。不断循环下去即可。

#include <set>#include <map>#include <queue>#include <stack>#include <cmath>#include <cstdio>#include <cstdlib>#include <iostream>#include <algorithm>#define EPS 1e-10#define MAX_N 30000#define Pi acos(-1.0)#define INF 0x3f3f3f3fusing namespace std;struct data{    double l, r;//一定要写成double    bool operator < (const data& b) const    {        return l < b.l || (l == b.l && r < b.r);    }};int n;double d;data dat[MAX_N];int solve(){    sort(dat, dat + n);    int res = 0;    double L = dat[0].l, R = dat[0].r;//刚开始写成int,wa了    for (int i = 0; i < n; i++)    {        //找相交区间        if (dat[i].l <= R && dat[i].r >= R)            L = dat[i].l;        else if (dat[i].l <= R && dat[i].r <= R)        {            L = dat[i].l;            R = dat[i].r;        }        else        {            res++;            L = dat[i].l;            R = dat[i].r;        }    }    return res + 1;}int main (){    //freopen ("in.txt", "r", stdin);    //freopen ("out.txt", "w", stdout);    int Case = 1;    while (~scanf("%d%lf", &n, &d))    {        if (!n && !d) break;        bool has_ans = true;        for (int i = 0; i < n; i++)        {            double x, y;            scanf("%lf%lf", &x, &y);            if (y > d)                has_ans = false;            else            {                dat[i].l = x - sqrt((double)(d * d - y * y));                dat[i].r = x + sqrt((double)(d * d - y * y));            }        }        printf("Case %d: ", Case++);        if (!has_ans || d <= 0)            printf("-1\n");        else            printf("%d\n", solve());    }    return 0;}


0 0
原创粉丝点击