Test instructions: give you n points, let you find the maximum three points collinear, 1000 points
The puzzle: Obviously, the point 1000, the Normal enumeration O (N3), definitely not.
Method for map to record the slope of each point and other point lines, if the slope occurs more than 2, Num + = C (n,2);
Code:
#include <stdio.h> #include <iostream> #include <map>using namespaceStd;Map <double , int> Mark; int Flag; struct Node { int X,Y; }Cun[1005]; void Slove(Node A,Node b ) { if ( A.X ==B.X) {Flag ++ ; return ;} Double K =(1.0 * (A.Y -B.Y)) / (1.0 * (A.X -B.X));Mark[K] ++; return ; }int Slov (int A ) { int K =A * (A-1) /2; return K; } int main() { int T,N;scanf("%d",&T); while (T--) {scanf("%d", &N);Mark.Clear(); for (int I =0;I<N;I++) {scanf("%d%d", &Cun[I].X, &Cun[I].Y); } __int64 Num =0;Flag =0; for (int I =0;I<N;I + +) { for(int J =I+1;J<N;J++) {Slove (Cun[I],Cun[J]); }Map <double, int;:: Iterator My_itr; for (My_itr=Mark.Begin();My_itr!=Mark.End();++My_itr ) { int K =My_itr -Second; if (K>=2)Num +=Slov(K); } if( Flag>=2)Num +=Slov(Flag);Flag =0;Mark.Clear(); }Printf("%i64d\n",Num); } }
HDU 5020 Container Mark Slope