hdu4946 Area of Mushroom(凸包)

hdu4946

题目

有n个人,每个人有一个坐标和速度,平面上如果的点如果他到达的时间严格的比其他任何人都快,那么这个点就属于他管辖。问每个人的管辖区域是不是无穷大。

思路

首先只有速达最大的那批人才有可能,把他们处理出来,然后对他们求凸包,在凸包里面的人是不可能的,在凸包边和顶点上的人是可以的,如果这些点钟有几个重合的,也要去掉。还有就是速度为零的点,不能算进去。

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

#define pi acos (-1)
#define rotate Rotate

using namespace std;

typedef long long ll;

const double eps = 1e-8;
const int maxn=600;
const double INF = 1e20;

int dcmp (double x)
{
    if (fabs (x) < eps)
        return 0;
    else return x < 0 ? -1 : 1;
}

struct point
{
    double x, y;
    int pos;
    point (double _x = 0, double _y = 0,int _pos=0) : x(_x), y(_y),pos(_pos) {}
    point operator - (point a) const
    {
        return point (x-a.x, y-a.y);
    }
    point operator + (point a) const
    {
        return point (x+a.x, y+a.y);
    }
    bool operator < (const point &a) const
    {
        return x < a.x || (x == a.x && y < a.y);
    }
    bool operator == (const point &a) const
    {
        return dcmp (x-a.x) == 0 && dcmp (y-a.y) == 0;
    }
};

point operator * (point a, double p)
{
    return point (a.x*p, a.y*p);
}
double cross (point a, point b)
{
    return a.x*b.y-a.y*b.x;
}
double dot (point a, point b)
{
    return a.x*b.x + a.y*b.y;
}

int ConvexHull (point *p, point *ch, int n)
{
    sort (p, p+n);
    int m = 0;
    for (int i = 0; i < n; i++)
    {
        while (m > 1 && cross (ch[m-1]-ch[m-2], p[i]-ch[m-1]) <= 0)
            m--;
        ch[m++] = p[i];
    }
    int k = m;
    for (int i = n-2; i >= 0; i--)
    {
        while (m > k && cross (ch[m-1]-ch[m-2], p[i]-ch[m-1]) <= 0)
            m--;
        ch[m++] = p[i];
    }
    if (n > 1)
        m--;
    return m;
}

int cnt,n,tot;
point p[maxn],ch[maxn];
int ans[maxn];
int kase;

bool OnSegment (point p, point a1, point a2)
{
    return dcmp (cross (a1-p, a2-p)) == 0 && dcmp (dot (a1-p, a2-p)) < 0;
}

void solve()
{
    memset(ans,0,sizeof(ans));
    for(int i=0; i<cnt; i++)
        ans[ch[i].pos]=1;

    for(int i=0; i<cnt; i++)
        for(int j=0; j<tot; j++)
        {
            if(OnSegment(p[j],ch[i],ch[(i+1)%cnt]))
                ans[p[j].pos]=1;
        }

    for(int i=0; i<tot; i++)
    {
        if(ans[p[i].pos]==1)
        {
            for(int j=0; j<tot; j++)
            {
                if(i!=j&&p[i]==p[j])
                    ans[p[i].pos]=0;
            }
        }
    }
    printf("Case #%d: ",kase++);
    for(int i=0; i<n; i++)
        if(ans[i]==1) printf("1");
        else if(ans[i]==0)printf("0");
    printf("\n");

}

int main()
{
    kase=1;
    while(scanf("%d",&n)&&n)
    {
        tot=0;
        double maxx=0;
        for(int i=0; i<n; i++)
        {
            double x,y,z;
            scanf("%lf %lf %lf",&x,&y,&z);
            if(z==0) continue;
            else if(maxx<z)
            {
                tot=0;
                p[tot++]=point(x,y,i);
                maxx=z;
            }
            else if(maxx==z)
            {
                p[tot++]=point(x,y,i);
            }
        }
        if(tot==0)
        {
            printf("Case #%d: ",kase++);
            for(int i=0; i<n; i++)
                printf("0");
            printf("\n");
        }
        else
        {
            cnt=ConvexHull(p,ch,tot);
            solve();
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值