Description
There are n distinct points in the plane, given by their integer coordinates. Find the number of parallelograms whose vertices lie on these points. In other words, find the number of 4-element subsets of these points that can be written as {A, B, C, D} such that AB || CD, and BC || AD. No four points are in a straight line.
Input
Input starts with an integer T (≤ 15), denoting the number of test cases.
The first line of each test case contains an integer n (1 ≤ n ≤ 1000). Each of the next n lines, contains 2 space-separated integers x and y (the coordinates of a point) with magnitude (absolute value) of no more than 1000000000.
Output
For each case, print the case number and the number of parallelograms that can be formed.
Sample Input
2
6
0 0
2 0
4 0
1 1
3 1
5 1
7
-2 -1
8 9
5 7
1 1
4 8
2 0
9 8
Sample Output
Case 1: 5
Case 2: 6
http://www.verydemo.com/demo_c116_i5169.html
思路:1)平行四边形的对角线的中点一定相交。<=> 如果有两条不同线段的中点相交,就是一个平行四边形 2)利用点坐标求出中点的集合,离散化后求出同个中点的出现的个数k。 3)对于每一个k ,利用组合公式C(k,2)的答案就是平行四边行的个数
思路:一个二维坐标系中给出n个点,可以两两连线,问这些所有线段中能组成多少平行四边形。n <= 1000。 //这里显然不能枚举组合,那样是n^4的做法,必然是超时的。那么我们可以用平四边形的等价定义, //两条相互平分的线段的四个点是平行四边形的顶点,那么我们可以先用n^2de方法求出任意两个点的连线 //(某四边形的duijiaoxian)的中点。这样就有n*(n-1)/2个中点。然后我们sort一下,求出相同的点的个数, //然后一这个点某平行四边形对角线的中点的四边形个数为x(x-1)/2。 //这样整体的时间复杂度是n^2*log(n)(排序)。 #include <iostream> #include <cstdio> #include <algorithm> using namespace std; struct node { int x,y; }data[1005],mid[1001005]; int n; int C (int k) //计算组合数 { return k*(k-1)/2; } bool cmp(node a,node b) { if (a.x == b.x) return a.y<b.y; return a.x<b.x; } void Deal () { int i,j,num=1,ans=0,id=-1; for (i=1;i<n;i++) for (j=i+1;j<=n;j++) { id++; mid[id].x=data[i].x+data[j].x; mid[id].y=data[i].y+data[j].y; } sort(mid,mid+id+1,cmp); //注意排序区间 for (i=0;i<id;i++) { if (mid[i].x==mid[i+1].x && mid[i].y==mid[i+1].y) num++; else { ans+=C(num); num=1; } } printf("%d\n",ans); } int main () { int t; scanf("%d",&t); int k=1; while(t--) { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d%d",&data[i].x,&data[i].y); printf("Case %d: ",k++); Deal (); } return 0; }