hdu 3629 Convex 平面点集能组成多少凸四边形 极角排序

Convex

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 369    Accepted Submission(s): 79

Problem Description
Your task is very simple. You will get the coordinates of n points in a plane. It is guaranteed that there are no three points on a straight line. You can choose any four points from these points to construct a quadrangle. Now, please tell me how many convex quadrangles you can construct.
 

 

Input
The first line of input contain an integer z (z ≤ 20), indicating the number of test cases.
For each test case, the first line contain an integer n (4 ≤ n ≤ 700), indicating the number of points. Each of the next n lines contains two integers x and y (-1000000 ≤ x, y ≤ 1000000), indicating the coordinate of corresponding point.

 

 

Output
For each test case, just output a single integer, the number of convex quadrangles you can construct.
 

 

Sample Input
  
  
2 4 0 0 0 1 1 0 1 1 4 0 0 1 0 0 1 -1 -1
 

 

Sample Output
  
  
1 0
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const double pi=3.1415926535;
struct Point
{
    double x,y;//注意用double  如果用int会超时
};
Point a[1000];
double t[2100];
double dis(Point a, Point b){     return sqrt(0.0+(a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));}
__int64 C(int n,int k)
{
    if(n<k) return 0;
    if(k==2) return (__int64)n*(n-1)/2;
    if(k==3) return (__int64)n*(n-1)*(n-2)/6;
    if(k==4) return (__int64)n*(n-1)*(n-2)*(n-3)/24;
}
int main()
{
    int ci;scanf("%d",&ci);
    while(ci--)
    {
        int n;scanf("%d",&n);
        for(int i=0;i<n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);//注意用double  如果用int会超时
        __int64 cnt=C(n,4);//四边形总个数
        for(int i=0;i<n;i++)
        {
            int pl=0;//极角排序
            for(int j=0;j<n;j++)
            {
                if(i==j) continue;
                t[pl++]=acos((a[j].x-a[i].x)/dis(a[i],a[j]));//acos  important
                if(a[j].y<a[i].y) t[pl-1]=2*pi-t[pl-1];//点在下方   important
            }
            sort(t,t+pl);//极角排序
            for(int i=0;i<pl;i++) t[i+pl]=t[i]+2*pi;//important
          __int64 res=C(n-1,3);//三角形总个数
            for(int i=0,j=1;i<pl&&j<2*pl;i++)
            {
                while(t[j]-t[i]<pi) j++;
                if(j-1-i>=2) res-=C(j-1-i,2);//C(j-1-i,2) 点在三角形外面的情况
            }
            cnt-=res;//res  点在三角形里面的情况
        }
        printf("%I64d/n",cnt);
    }
    return 0;
}
/*
如果四个点不能组成凸四边形,则必然是其中三个点组成一个三角形,另一个点在该三角形内部。
于是我们可以O(n)枚举一个点作为内部中心,试图从其他的点里选出三个来,组成三角形把它包围住,
看看有多少种可能的选择。继续观察发现,如果三个点不能圈住中心点,则必然是存在一条通过中心点的直线,
使得这三点都在直线的同侧。于是我们可以把所有点(除了中心点)按极角排序,然后线性转圈扫描一下就可以统计出来了。
总的复杂度是O(n^2*logn)
*/
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值