http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18277
Plane to n points, guaranteed no three points collinear
If there are x points inside a triangle, contribute x to the contribution of all triangles.
The direct number of points within each triangle is not good, you can reverse the "how many triangles each point contains"
Enumerate each point x, centering the point, and sorting the rest of the points by the polar order,
So for the point J of the Polar Order Y, we find the first point z of the polar order is greater than Y+PI, then from the y+1 to z-1 all points in the choice of two, you can and point y form a "no point x" triangle, so for all points, you can find all the triangle without a point x.
If the angle of two points >pi, does not constitute "the triangle does not contain X", but 2pi-angle is <pi, that is, the other side, so you need to copy the array to twice times
Looking for "The first point z of the polar order is greater than Y+PI," Here I lazy with two points, the total complexity is n (NLGN+NLG)
It is better if it is N (lgn+n) with the pointers.
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm&
Gt #include <queue> #include <map> #include <set> #include <vector> #include <iostream> using
namespace Std;
Const double Pi=acos (-1.0);
Double eps=0.000001;
int min (int a,int b) {return a<b?a:b;}
int max (int a,int b) {return a>b?a:b;}
struct node {int x, y;
Node () {} double Jijiao;
node (int a,int b) {x=a,y=b;}};
Node tm[1255];
Node tmp[2455];
BOOL CMP (node A,node b) {return a.jijiao<b.jijiao;} int n;
Double count (int x) {int i;
int idx=1;
for (i=1;i<=n;i++) {if (i==x) continue;
tmp[idx].x=tm[i].x-tm[x].x;
TMP[IDX].Y=TM[I].Y-TM[X].Y;
Tmp[idx].jijiao=atan2 (tmp[idx].y,tmp[idx].x);
Tmp[idx+n-1].jijiao=tmp[idx].jijiao+2*pi;
idx++;
} sort (tmp+1,tmp+1+ (2*n-2), CMP);
node TP;
Double res=0;
for (i=1;i<n;i++) {tp.jijiao=tmp[i].jijiao+pi;
int It=upper_bound (TMP+1,TMP+1+2*N-2,TP,CMP)-tmp; IntCnt=it-i-1;
res+= cnt* (cnt-1)/2;
} double s= (n-1) * (n-2) * (n-3)/6.0;
return s-res;
} int main () {int cnt=1;
int i;
while (1) {cin>>n;
if (!n) break;
for (i=1;i<=n;i++) {scanf ("%d%d", &tm[i].x,&tm[i].y);
} double ans=0;
for (i=1;i<=n;i++) ans+=count (i);
printf ("City%d:%.2lf\n", cnt++,ans/n/(n-1)/(n-2) *6.0);
} return 0; }