Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 | Output for 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 | Case 1: 5 Case 2: 6 |
SPECIAL THANKS: JANE ALAM JAN (DESCRIPTION, SOLUTION, DATASET)
题目大意:给你若干个点的坐标,问这些点随机组合之后,能得到多少个平行四边形。
解题思路:按照斜率、向量的思路的话会重复或遗漏,这里想想,平行四边形两条对角线交点固定,即两两点中点值一样,那么就好办了,如果n对点中点值一样的话,那么从中任选两对,c(n,2),就能求出当前组合的平行四边形个数了。
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
struct node
{
int x,y;
}dian[1010];
node mid[500010];
bool cmp(struct node a,struct node b)//排序
{
if(a.x==b.x)
{
return a.y<b.y;
}
else
{
return a.x<b.x;
}
}
int main()
{
int t;
scanf("%d",&t);
int k=1;
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&dian[i].x,&dian[i].y);
}
int num=0;
for(int i=0;i<n;i++)//两两点组合成对
{
for(int j=i+1;j<n;j++)
{
mid[num].x=dian[i].x+dian[j].x;
mid[num].y=dian[i].y+dian[j].y;
num++;
}
}
sort(mid,mid+num,cmp);//排下序,以为要计算n对中点相同的点的对数
int geshu=1;
int ans=0;
int flag=0;//作为区间的起点
for(int i=1;i<num;i++)
{
if(mid[i].x==mid[flag].x&&mid[i].y==mid[flag].y)//统计n为多少
{
geshu++;
}
else
{
ans=ans+geshu*(geshu-1)/2;
geshu=1;//再次初始化
flag=i;//更新flag
}
}
printf("Case %d: %d\n",k++,ans);
}
return 0;
}