您的位置:首页 > 其它

poj 1971 Parallelogram Counting

2014-09-30 10:51 190 查看
Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 5814Accepted: 1961
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

The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases. It is followed by the input data for each test case.

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

Output should contain t lines.

Line i contains an integer showing the number of the parallelograms as described above for test case i.

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
5
6




题意:给出n个点,求出这n个点能够组成平行四边形的个数。

思路 :        这题需要转化一下,首先要先考虑四个点能构成平行四边形所需的条件;        不难想到: 平行四边形的对角线的中点一定相交。所以说  如果有两条不同线段的中点相同,那么着两条线段就可以构成一个平行四边形。        即我们只要统计所有线段的中点,然后排个序,找出每个中点相同的次数 coun ,最后对于每一个coun ,coun*(coun-1)/2 就是能构成平行四边行的个数。
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=1010;

struct node
{
int x,y;
} a[maxn],mid[maxn*maxn];
int n,cnt,T;

bool cmp(node p,node q)
{
if(p.x!=q.x)  return p.x<q.x;
return p.y<q.y;
}

void input()
{
scanf("%d",&n);
for(int i=0; i<n; i++)  scanf("%d %d",&a[i].x,&a[i].y);
}

void solve()
{
cnt=0;
for(int i=0; i<n; i++)
for(int j=0; j<i; j++)
{
mid[cnt].x=a[i].x+a[j].x;
mid[cnt++].y=a[i].y+a[j].y;
}
sort(mid,mid+cnt,cmp);
int coun=1,ans=0;
for(int i=1;i<cnt;i++)
{
if(mid[i].x!=mid[i-1].x || mid[i].y!=mid[i-1].y)
{
ans=ans+(coun-1)*coun/2;
coun=1;
}
else coun++;
}
ans=ans+(coun-1)*coun/2;
printf("%d\n",ans);
}

int main()
{
scanf("%d",&T);
for(int co=1; co<=T; co++)
{
input();
solve();
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: