Revenge of collinearityTime
limit:8000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Problem DescriptionIn geometry, collinearity is a property of a set of points, specifically, the property of lying on a Si Ngle line. A set of points with the is said to be collinear (often misspelled as colinear).
---Wikipedia
Today, collinearity takes revenge on you. Given a set of N points in two-dimensional coordinate system, you have to find how many set of <pi, Pj, pk> from the Se N points is collinear. Note that <pi, PJ, pk> cannot contains same point, and <pi, Pj, pk> and <pi, Pk, pj> is considered as T He same set, i.e. the order in the set doesn ' t matter.
Inputthe first line contains a single integer T, indicating the number of test cases.
Each test case is begins with an integer n, following n lines, each line contains the integers Xi and Yi, and describing a point.
[Technical specification]
1.1 <= T <= 33
2.3 <= N <= 1 000
3.-1 <= Xi, Yi <= 1, and No, points is identical.
4. The ratio of test cases with N > are less than 25%.
Outputfor each query, output the number of three points set which is collinear.
Sample Input
231 12 23 340 01 00 11 1
Sample Output
10
Sourcebestcoder Round #10
topic: Give the point of N two-dimensional plane, ask how many pairs of three points collinear.
problem-solving ideas: Since it is three points collinear, it may be set to a,b,c three points, if we are purely violent enumeration a,b,c three points, for ABC three points do not repeat each other, we can first order, according to X priority non-descending, y followed by non-decrement. But pure violence must be tle, if you reduce the number of enumeration points, then you can reduce the complexity of time, if we only enumerate point A, then for each of the other points will be a straight line with point A, if a line appears more than once, then it means that there are at least two other pairs and a point in the same line, Then the corresponding existence of three points collinear. Suppose there are k points and a-point
collinear (Note: Here the
collinear is that the k+1 points are in the same line) (also note that the K-points are not duplicated, what is not repeated?) Examples: there are (1,0), (2,0), (3,0), (4,0), (5,0) Five points when a= (1,0), k=4 when a= (2,0), k=3 instead of k=4; when A=(3,0), k=2, ... )。 Because the enumeration is a point,where a point must be included, then there is k* (k-1)/2 pairs of three points collinear, enumerate all a points, the sum is the answer. for the solution of the number of collinear k ..... ....... ..............
The code is as follows:
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <iostream> #include <algorithm> #include <string> #include <vector> #include <deque > #include <list> #include <set> #include <map> #include <stack> #include <queue># Include <numeric> #include <iomanip> #include <bitset> #include <sstream> #include <fstream > #include <limits.h> #define DEBUG "Output for debug\n" #define PI (ACOs ( -1.0)) #define EPS (1e-6) #define INF (1< ; <28) #define SQR (x) (x) * (x) #define MoD 1000000007using namespace std;typedef long long ll;typedef unsigned long long Ull;typedef pair<ll,ll> p;map<p,ll> m;struct point{ll x, y;} A[1005];ll gcd (ll A,ll b) {return B?GCD (b,a%b): A;} BOOL CMP (point A,point B) {if (a.x==b.x) return a.y<b.y; return a.x<b.x;} int main () {ll i,j,k,n,t; scanf ("%i64d", &t); while (t--) {SCANF ("%i64d", &n); for (i=0;i<n;i++) scanf ("%i64d%i64d", &a[i].x,&a[i].y); Sort (a,a+n,cmp); ll Ans=0; for (i=0;i<n;i++) {m.clear (); for (j=i+1;j<n;j++) {ll dx=a[j].x-a[i].x; ll Dy=a[j].y-a[i].y; ll D=GCD (Dx,dy); dx=dx/d; dy=dy/d; M[make_pair (Dx,dy)]++; } Map<p,ll>::iterator it; For (It=m.begin (); It!=m.end (); it++) {k=it->second; if (k>=2) ans=ans+k* (k-1)/2; }} printf ("%i64d\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5020 Revenge of collinearity (enumeration)