, ( ), , , , . n log n, , , Dynamic Programming/DaC.
. , .
-, . , , , .
.
, , , , .
, , , .
, , , . , , " (0,0) S", , .
, , , , , , , , . , S , , .
, .
.
, , , , , .
, :
S. A, (atan2 (x, y)) A (0 β€ Ai β€ 2Ο). , O (n)
. O (n log n), , Merge Sort.
, (Ai, Aj). , Ai + Ο β€ Ak β€ Aj + Ο. , , Ai + Ο Aj + Ο, O (2 log n) = O (log n)
, n ^ 2 , O (log n), O (n ^ 2 log n). , .
Ai < Aj, , Tij , . , Ak > Aj, , Tij β€ Tik, Ai + Ο Ak + Ο , Ai + Ο Aj + Ο. , Ai + Ο Aj + Ο, Aj + Ο Ak + Ο. Ai + Ο Aj + Ο, - Aj + Ο Ak + Ο, . , :
A (n) = count (A (n), A (n-1)) + count (A (n-1), A (n-2)) +... + count (A (1), (0))
, n ^ 2 , - n-1.
, psuedocode.
int triangleCount(point P[],int n)
int A[n], C[n], totalCount = 0;
for(i=0...n)
A[i] = atan2(P[i].x,P[i].y);
mergeSort(A);
int midPoint = binarySearch(A,Ο);
for(i=0...midPoint-1)
int left = A[i] + Ο, right = A[i+1] + Ο;
C[i] = binarySearch(a,right) - binarySearch(a,left);
for(j=0...i)
totalCount += C[j]
return totalCount;