bzoj1914[Usaco2010 OPen]Triangle Counting 数三角形 极角排序+乱搞

来源:互联网 发布:如何判定论文数据造假 编辑:程序博客网 时间:2024/06/15 00:31

题意:给出一些点,问你有多少个三角形能覆盖原点。
我们首先可以通过正难则反的原则,把题目转化为求不能覆盖原点的方案数。
总方案数是n*(n-1)*(n-2)/6.
先把所有点极角排序,然后扫过去,我们对于一个当前点i,他和原点的连线一定能把他所在的象限分成两个平面,对于线段左边的一个平面(当然你也可以选择右边),其中任意两个点x,y和i相连,肯定不会覆盖原点,所以通过这种方法就能够做到较快统计答案,而且不会算重算漏。
注意开ll
code:

#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#define fo(i,a,b) for(int i=a;i<=b;i++)#define fd(i,a,b) for(int i=a;i>=b;i--)using namespace std;int n,m;typedef long long ll;const int N=2e5+5;struct node{    ll x,y;    double z;}a[N];ll ans;bool cmp(node a,node b){    return a.z<b.z;}ll operator*(node a,node b){    return a.x*b.y-a.y*b.x;}bool operator<(node a,node b){    return a.z<b.z;}int main(){    scanf("%d",&n);    fo(i,1,n)    {        scanf("%lld%lld",&a[i].x,&a[i].y);        a[i].z=atan2(a[i].y,a[i].x);    }    sort(a+1,a+1+n);    int r=1,t=0;    fo(i,1,n)    {        while ((r%n+1)!=i&&a[i]*a[r%n+1]>=0)t++,r++;        ans+=(ll)t*(t-1)/2;        t--;    };    printf("%lld\n",(ll)n*(n-1)*(n-2)/6-ans);    return 0;}
0 0