您的位置:首页 > 其它

POJ 1971 Parallelogram Counting【平面几何】

2016-03-07 21:30 302 查看
Parallelogram Counting

Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 6073Accepted: 2072
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个点的坐标,求这些坐标能构成多少个平行四边形。开始看的用向量做,总是WA也找不到哪错,向量相等且不在一条直线上的向量的对数除2不就是结果?又用的中点做,中点相同的两条线段一定可以组成一平行四边形。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxn 1010
using namespace std;
struct Node
{
int x,y;
};
struct Edge
{
int a,b;
Node e;
};
bool cmp(Edge a,Edge b)
{
if(a.e.x==b.e.x)
return a.e.y<b.e.y;
return a.e.x<b.e.x;
}
Edge edge[maxn*maxn/2];
Node node[maxn];
int main()
{
int T,n;
scanf("%d",&T);
for(int t=1;t<=T;++t)
{
scanf("%d",&n);
for(int i=0;i<n;++i)
scanf("%d%d",&node[i].x,&node[i].y);
int k=0;
for(int i=0;i<n-1;++i)
{
for(int j=i+1;j<n;++j)
{
edge[k].a=i;
edge[k].b=j;
edge[k].e.x=(node[i].x+node[j].x);
edge[k].e.y=(node[i].y+node[j].y);
k++;
}
}
sort(edge,edge+k,cmp);
int ans=0;
for(int i=0;i<k;++i)
{
int j=i;
int num=0;
while(j<k&&edge[j].e.x==edge[i].e.x&&edge[j].e.y==edge[i].e.y)
{
j++;
num++;
}
ans+=num*(num-1)/2;
i=j-1;
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: