【BZOJ1914】数三角形(组合数,计算几何)

题面

BZOJ权限题
良心洛谷

题解

这种姿势很吼啊,表示计算几何啥的一窍不通来着。
题目就是这样,正难则反,所以我们不考虑过原点的三角形,
反过来,总数减去不包含原点的三角形。
这个怎么计算呢?
我们每次先确定一个点,那么,所有在这个点和原点的连线下方的点都是可行的,
那么极角排序之后发现这就是一段连续的区间,
所以直接线性扫一遍就好了,组合数算算就没问题了。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
#define MAX 100100
inline int read()
{
    int x=0;bool t=false;char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
    if(ch=='-')t=true,ch=getchar();
    while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
    return t?-x:x;
}
struct Node{int x,y;double d;}a[MAX];
bool operator<(Node a,Node b){return a.d<b.d;}
bool Slope(Node a,Node b){return 1ll*a.x*b.y-1ll*a.y*b.x>=0;}
int n;ll ans;
int main()
{
    n=read();ans=1ll*n*(n-1)*(n-2)/6;
    for(int i=0;i<n;++i)a[i].x=read(),a[i].y=read(),a[i].d=atan2(a[i].y,a[i].x);
    sort(&a[0],&a[n]);
    for(int i=0,r=0,t=0;i<n;++i)
    {
        while((r+1)%n!=i&&Slope(a[i],a[(r+1)%n]))++t,++r;
        ans-=1ll*t*(t-1)/2;--t;
    }
    printf("%lld\n",ans);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值