poj1328 贪心

Posted a_clown_cz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj1328 贪心相关的知识,希望对你有一定的参考价值。

http://http://poj.org/problem?id=1328

神TM贪心。

不懂请自行搜博客。

AC代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
struct Node
{
    double l;
    double r;
} node[2010],temp;
int cmp(Node x,Node y)
{
    return x.l<y.l;
}
int main()
{
    int n,num=0;
    double d,a,b;
    bool flag;
    while( scanf("%d%lf",&n,&d)==2 && (n||d) )
    {
        flag=false;
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf",&a,&b);
            node[i].l=a-sqrt(d*d-b*b);
            node[i].r=a+sqrt(d*d-b*b);
            if(fabs(b)>d) flag=true;
        }
        if(flag==true) printf("Case %d: -1\n",++num);
        else
        {
            sort(node,node+n,cmp);
            temp=node[0];
            int ans=1;

            for(int i=1; i<n; i++)
            {
                if(node[i].l>temp.r)
                {
                    ans++;
                    temp=node[i];
                }
                else if(node[i].r<temp.r)
                    temp=node[i];
            }
            printf("Case %d: %d\n",++num,ans);
        }
    }
    return 0;
}

 

以上是关于poj1328 贪心的主要内容,如果未能解决你的问题,请参考以下文章

POJ 1328 Radar Installation 贪心算法

贪心-poj1328

POJ 1328 Radar Installation 贪心题解

POJ 1328 Radar Installation(贪心)

poj 1328贪心

贪心poj1328:雷达设置