bzoj1914 Triangle Counting 数三角形【极角排序】

解题思路:

可以发现,对于一个不是黄金三角形的三角形,有且仅有两个顶点满足:另外两个顶点在其与原点连线同侧。所以我们可以用所有三角形个数( C3n )减去不满足的个数得到答案。

将所有点极角排序,枚举每个点,为了去重对于一个顶点只算在其与原点连线右侧的点数,那么不合法的三角形数就是 C2n ,又注意到随着点的枚举,不合法的点也是随之单调旋转的,所以用两个指针即可。

PS:
原来极角排序可以用atan2(y,x)函数;
它可以算出向量(x,y)与x正半轴的弧度,值域为[- ππ ]。
感觉计算几何白学了……我以前自己写细节调到吐血……

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<vector>
#include<set>
#define ll long long
using namespace std;

int getint()
{
    int i=0,f=1;char c;
    for(c=getchar();(c!='-')&&(c<'0'||c>'9');c=getchar());
    if(c=='-')f=-1,c=getchar();
    for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
    return i*f;
}

const int N=100000;
struct point
{
    int x,y;
    double k;
    inline friend bool operator < (const point &a,const point &b)
    {return a.k<b.k;}
    inline friend ll operator * (const point &a,const point &b)
    {return 1ll*a.x*b.y-1ll*a.y*b.x;}
}p[N];
int n;
ll ans;

int main()
{
    //freopen("lx.in","r",stdin);
    n=getint();ans=1ll*n*(n-1)*(n-2)/6;
    for(int i=0;i<n;i++)
        p[i].x=getint(),p[i].y=getint(),p[i].k=atan2(p[i].y,p[i].x);
    sort(p,p+n);
    for(int i=0,cnt=0,j=0;i<n;i++)
    {
        if(cnt)cnt--;
        while(p[i]*p[(j+1)%n]>0)
            cnt++,j=(j+1)%n;
        ans-=1ll*cnt*(cnt-1)/2;
    }
    cout<<ans<<'\n';
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值