hdu 5761 How Many Triangles 极角排序

http://acm.hdu.edu.cn/showproblem.php?pid=5784

暴力每一个点,然后以这个点为原点做一个极角排序,尺取法求有多少个钝角,平角等等,为了不重复枚举,可以对每个点都只算向左180度以内的(利用叉乘正负判断)


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct point
{
    long long x,y;
    point(ll x=0,ll y=0):x(x),y(y){}
    point operator - (const point &t)const
    {
        return point(x-t.x,y-t.y);
    }
    ll operator * (const point &t)const//有向面积,逆时针为正
    {
        return x*t.y-y*t.x;
    }
    ll operator ^ (const point &t)const
    {
        return t.x*x+t.y*y;
    }
    bool operator < (const point &t)const
    {
        int t1=0,t2=0;
        if(y>0||y==0&&x>0)t1=1;
        if(t.y>0||t.y==0&&t.x>0)t2=1;
        if(t1!=t2)
            return t1>t2;
        if((*this)*t==0)
            return ((*this)^(*this))<(t^t);
        return (*this)*t>0;
    }
}p[2005],te[4005];
int main()
{
    int n,i,j;
    ll ans,tmp;
    while(cin>>n)
    {
        ans=0,tmp=0;
        for(i=0;i<n;i++)
        scanf("%lld %lld",&p[i].x,&p[i].y);
        for(i=0;i<n;i++)
        {
            int tot=0;
            for(j=0;j<n;j++)
            {
                if(j!=i)
                {
                    te[tot++]=p[j]-p[i];
                }
            }
            sort(te,te+tot);
            for(j=0;j<tot;j++)
            {
                te[j+tot]=te[j];
            }
            int now=0;
            for(j=1;j<tot;j++)//0度角个数
            {
                if((te[j]*te[j-1])==0&&(te[j]^te[j-1])>0)
                now++;
                else
                now=0;
                tmp+=now;
            }
            int i1=1,i2=1;
            for(j=0;j<tot;j++)
            {
                i1=max(i1,j+1);
                while(i1<j+tot&&(te[j]*te[i1])>=0&&(te[j]^te[i1])>0)
                    i1++;
                i2=max(i2,i1);
                while(i2<j+tot&&(te[j]*te[i2])>0&&(te[j]^te[i2])<=0)
                    i2++;
                ans+=(i2-i1);
            }
        }
        cout<<1ll*n*(n-1)*(n-2)/6-ans-tmp/2<<endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值