问题实质上是求平面上的n个点一共可以构成多少个凹四边形,方法就是先选定一个点作为原点,然后将剩下的点级角排序,然后扫一遍,统计在原点的一侧共能形成多少个三角形,即凹四边形的个数。累加即为最终的结果。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=3000;
const double pi=acos(-1.0);
struct point
{
double x,y;
point(double x=0,double y=0):x(x),y(y){}
} p[maxn];
double a[maxn];
int n;
int main()
{
//freopen("in.txt","r",stdin);
int kase=1;
while(~scanf("%d",&n) && n)
{
for(int i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
LL ans=0,tmp=(n-1)*(n-2)*(n-3)/6;
for(int i=0;i<n;i++)
{
if(i!=0) swap(p[i],p[0]);
for(int j=1;j<n;j++)
a[j]=atan2(p[j].y-p[0].y,p[j].x-p[0].x);
sort(a+1,a+n);
for(int j=1;j<n;j++) a[j+n-1]=a[j]+2*pi;
int q=1;
LL res=0;
for(int j=1;j<n;j++)
{
while(a[q]<a[j]+pi) q++;
res+=(q-j-1)*(q-j-2)/2;
}
ans+=(tmp-res);
}
double a=ans*6.0/n/(n-1)/(n-2);
printf("City %d: %.2f\n",kase++,a);
}
return 0;
}