您的位置:首页 > 其它

POJ---1791 Parallelogram Counting[数学题-平行四边形求个数]

2012-08-23 00:07 281 查看
Parallelogram Counting

Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 5361Accepted: 1794
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

Source

Tehran Sharif 2004 Preliminary

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

【解题思路】:

1)平行四边形的对角线的中点一定相交。<=> 如果有两条不同线段的中点相交,就是一个平行四边形

2)利用点坐标求出中点的集合,离散化后求出同个中点的出现的个数k。
3)对于每一个k ,利用组合公式C(k,2)的答案就是平行四边行的个数

code:

#include<iostream>
#include<algorithm>
using namespace std;

#define MAXN 1010

typedef struct point
{
int x,y;
}Point;
Point point[MAXN];
Point mid[MAXN*MAXN];

int cmp(const Point &a,const Point &b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}

int main()
{
int t;
int i,j;
int sum;
int n;
int cnt;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
sum=0;
cnt=0;
for(i=0;i<n;i++)
scanf("%d%d",&point[i].x,&point[i].y);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
mid[cnt].x=(point[i].x+point[j].x);
mid[cnt].y=(point[i].y+point[j].y);
cnt++;
}
sort(mid,mid+cnt,cmp);
int count=1;
for(i=0;i<cnt;i++)
{
if(mid[i].x==mid[i+1].x&&mid[i].y==mid[i+1].y)
count++;
else
{
sum+=(count-1)*count/2;
count=1;
}
}
printf("%d\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: