首页 > 其他 > 详细

POJ 1328 Radar Installation#贪心(坐标几何题)

时间:2016-02-28 00:49:06      阅读:261      评论:0      收藏:0      [点我收藏+]

(~ ̄▽ ̄)~*

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

struct node
{
    double l,r;
    //找到以岛为圆心,以d为半径的圆与坐标x轴的左交点l、右交点r
    //雷达只有设在l~r之间,岛才在雷达覆盖范围内
}a[1005];

int cmp(node a,node b)
{
    return a.l<b.l;
}

int main()
{
    int n,d;
    double right;
    int Cas=0;
    while(scanf("%d%d",&n,&d)&&n+d)
    {
        Cas++;
        bool flag=1;
        for(int i=0;i<n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            a[i].l=x-sqrt(d*d*1.0-y*y*1.0);
            a[i].r=x+sqrt(d*d*1.0-y*y*1.0);
            int dis;
            if(y<0) dis=-y;
            else dis=y;
            if(dis>d) flag=0;
        }
        if(!flag) printf("Case %d: -1\n",Cas);
        else
        {
            int ans=1;
            sort(a,a+n,cmp);
            right=a[0].r;
            for(int i=1;i<n;i++)
            {
                if(a[i].l>right)
                {
                    ans++;
                    right=a[i].r;
                }
                if(a[i].r<right)
                    right=a[i].r;
            }
            printf("Case %d: %d\n",Cas,ans);
        }
    }
    return 0;
}

POJ 1328 Radar Installation#贪心(坐标几何题)

原文:http://www.cnblogs.com/atmacmer/p/5223851.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!