平行四边形数(fzoj_2231) 几何

 Problem 2231 平行四边形数

Accept: 44    Submit: 124
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

 Source


解题思路:平行四边形的中点平分两条对角线;所以求有多少重叠的中点,再用排列组合;

代码如下:

#include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;

typedef struct p{
	double x, y;
}point;
point a[505];
point zd[250005];

bool cmp(point v,point u){
	if(v.x == u.x) return v.y > u.y;
	return v.x > u.x;
}

int main(){
	int n, r;
	int ans, cnt;
	while(scanf("%d",&n) != EOF){
		r = 0;
		ans = 0;
		for(int i = 0;i < n;i ++)
			scanf("%lf%lf",&a[i].x, &a[i].y);
		for(int i = 0;i < n;i ++)
			for(int j = i + 1;j < n;j ++){
				zd[r].x = (a[i].x + a[j].x) / 2;
				zd[r ++].y = (a[i].y + a[j].y) / 2;
			}
		sort(zd,zd+r-1,cmp);
		point t = zd[0];
		cnt = 1;
		for(int i = 1;i < r;i ++){
			if(t.x == zd[i].x && t.y == zd[i].y)
				cnt ++;
			else{
				ans += cnt * (cnt -1) / 2;
				cnt = 1;
				t = zd[i];
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}


你可能感兴趣的:(ACM)