HDU3629(凸四边形的个数)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3629

 

题意:平面上给n个点,问有多少个凸四边形?

 

分析:对于每个点,凹四边形的个数等于:C(n-1,3)-在这个点同一侧三点构成的三角形的个数。对于凸多边形的一个顶点,

其他顶点必然在穿过这个顶点的直线的同侧。

 

算极角时,如果是负数(-pi ~ 0),就把它加上2 * pi,这样就把角度统一到了0~2pi,另外,向这题顺次统计两个点的夹

角时,由于会出现转了一圈的情况不好计算角度,所以在原来数组后面再顺次加上n-1一个点,角度同一加2pi

 

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>

using namespace std;
typedef long long LL;
const int N = 750;
const double PI = acos(-1.0);

struct Point
{
    int x,y;
};

Point p[N];
double A[N];
int n;

double angle(double x,double y)
{
    double t = y - x;
    if(t < 0) t += 2*PI;
    return t;
}

LL work()
{
    LL t1 = (LL)n*(n-1)*(n-2)*(n-3)/24;
    for(int k=0; k<n; k++)
    {
        int cnt = 0;
        for(int i=0; i<n; i++)
        {
            if(k != i)
                A[cnt++] = atan2((double)(p[i].y-p[k].y),(double)(p[i].x-p[k].x));
        }
        sort(A,A+n-1);
        LL t2 = (LL)(n-1)*(n-2)*(n-3)/6;
        for(int j=0,i=0; i<n-1; i++)
        {
            while(j<i+n-1)
            {
                if(angle(A[i],A[j%(n-1)])>PI) break;
                j++;
            }
            t2 -= (LL)(j-i-1)*(j-i-2)/2;
        }
        t1 -= t2;
    }
    return t1;
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n;
        for(int i=0; i<n; i++)
            cin>>p[i].x>>p[i].y;
        cout<<work()<<endl;
    }
    return 0;
}


 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值