Problem 2231 平行四边形数

来源:互联网 发布:浩方魔兽不能连入网络 编辑:程序博客网 时间:2024/05/22 08:38
Problem 2231 平行四边形数

Accept: 331    Submit: 1158
Time Limit: 2000 mSec    Memory Limit : 32768 KB

Problem Description

在一个平面内给定n个点,任意三个点不在同一条直线上,用这些点可以构成多少个平行四边形?一个点可以同时属于多个平行四边形。

Input

多组数据(<=10),处理到EOF。

每组数据第一行一个整数n(4<=n<=500)。接下来n行每行两个整数xi,yi(0<=xi,yi<=1e9),表示每个点的坐标。

Output

每组数据输出一个整数,表示用这些点能构成多少个平行四边形。

Sample Input

40 11 01 12 0

Sample Output

1

这个题是叫求平行四边形的个数,因为他每三个点不在一条直线上,所以只用求出其中点就行,中点相同就是平行四边形。注意:当n个点为同一个中点时,这时就有n(n-1)/2个平行四边形。
#include<cstdio>
#include<algorithm>
using namespace std;
struct coordinate{
int x;
int y;
}a[510],b[250010];
bool cmp(coordinate a,coordinate b)
{
if(a.x==b.x)
return a.y < b.y;
return a.x < b.x;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
if(n==0)
break;
   for(int i = 0; i < n; i++){
    scanf("%d%d", &a[i].x, &a[i].y);
}
int k = 0,ans = 0,sum=0;
for(int i = 0; i < n; i++)
{
for(int j = i+1;j < n; j++)
{
b[k].x = a[i].x + a[j].x;
b[k].y = a[i].y + a[j].y;
k++;
}
}

sort(b, b+k, cmp);
for(int i = 0; i < k-1; i++){
if(b[i].x == b[i+1].x && b[i].y == b[i+1].y )
ans++; 
else
{
sum+=ans*(ans+1)/2;

ans=0;
}
/*
*要注意有可能中点相同时有多个点符合题意,
*所以要选两个点,共有n*(n+1)/2方法。
*/ 
// printf("%d %d\n",sum,ans);
//printf("%d %d\n",b[i].x,b[i].y);
}
printf("%d\n",sum);
}
}