题目: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;
}