Problem how many triangles (HDU 5784)
Main topic
N Dots (n "2000) on a given plane, asking how many acute triangle can be composed.
Analysis of Problem solving
Direct statistics Acute triangle more difficult, consider the opposite side of the problem, statistics right triangle, obtuse triangle, the triangle (temporarily so called Qaq).
First, an endpoint a of the triangle is enumerated, the quadrant for the other points is the first keyword, and the polar angle is the second keyword sort.
Then use three pointers to scan O (n).
The concrete approach is to use the I pointer to point to the second endpoint of the triangle B. We can imagine that by panning and rotating, the point A is placed at the origin of the planar Cartesian coordinate system, and the B point is placed in the positive direction of the x-axis. Then the point that can be formed with ab obtuse or right angle is in the three or four quadrant or the Y axis.
Point the J pointer to the most posterior point in the first quadrant that can form a sharp angle, scan the K-pointer from J + 1 to the last point that can form an obtuse, and then count the contribution to the answer.
After the I pointer +1, continue scanning.
Note Some special situations, can write a violent to pat, make point small data Debug.
Analysis of Problem solving
1#include <cstdio>2#include <cstring>3#include <cmath>4#include <algorithm>5 using namespacestd;6 #defineEPS 1e-87 8 structp{9 Long Longx, y;Ten intF; OneFriend Poperator-(P a,p b) { A returnP {a.x-b.x,a.y-b.y}; - } -}a[20008],b[20008],s; the - intn,m; - -InlineLong LongCross (P a,p b,p c) {//AB X ac + return(b.x-a.x) * (C.Y-A.Y)-(B.Y-A.Y) * (c.x-a.x); - } + AInlineintCalc (P a) { at if(a.x>0&& a.y==0)return 1; - if(a.x>0&& a.y>0)return 1; - if(a.x==0&& a.y>0)return 2; - if(a.x<0&& a.y>0)return 2; - if(a.x<0&& a.y==0)return 3; - if(a.x<0&& a.y<0)return 3; in if(a.x==0&& a.y<0)return 4; - if(a.x>0&& a.y<0)return 4; to } + -InlineBOOLcmpConstP A,ConstP B) { the if(A.F<B.F)return true; * if(A.F>B.F)return false; $ Long Longtmp=Cross (s,a,b);Panax Notoginseng if(tmp>0)return true; - return false; the } + AInlineBOOLOK (P a,p b,p c) { the Long Longtmp= (b.x-a.x) * (c.x-a.x) + (B.Y-A.Y) * (c.y-a.y); + if(tmp>0)return true; - return false; $ } $ - intMain () { - while(~SCANF ("%d",&N)) { the for(intI=1; i<=n;i++) scanf ("%i64d%i64d",&a[i].x,&a[i].y); - Long Longsum=0;Wuyi for(intIi=1; ii<=n;ii++){ theS=A[II]; m=0; - for(intj=1; j<=n;j++) Wu if(J!=II) b[++m]=A[j]; - for(intI=1; i<=m;i++) B[i].f=calc (b[i]-S); AboutSort (b +1, b+m+1, CMP); $ intI=1, j=1, k=1; - while(OK (s,b[i],b[j]) && Cross (S,b[i],b[j]) >=0&& j<=m) J + +; - if(j==m+1)Continue; -j--; k=j+1; A while(i<=m) + { the if(!OK (S,b[i],b[k])) { - while(!ok (s,b[i],b[k+1]) && k<m) k++; $sum+=k-J; the } thei++; the if(j<i) j=i; the while(OK (s,b[i],b[j+1]) && Cross (s,b[i],b[j+1]) >0&& j<m) J + +; - if(K<=J) k=j+1; in if(k>m) Break; the } the } About Long Longp=1ll*n* (n1) * (n2)/6; theprintf"%i64d\n", P-sum); the } the}
View Code
HDU 5784 (Computational geometry)