Codeforces--660D--Number of Parallelograms(几何性质)(组合数)

来源:互联网 发布:资料恢复软件 编辑:程序博客网 时间:2024/06/06 02:02
Number of Parallelograms
Time Limit: 4000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u

Submit Status

Description

You are given n points on a plane. All the points are distinct and no three of them lie on the same line. Find the number of parallelograms with the vertices at the given points.

Input

The first line of the input contains integer n (1 ≤ n ≤ 2000) — the number of points.

Each of the next n lines contains two integers (xi, yi) (0 ≤ xi, yi ≤ 109) — the coordinates of the i-th point.

Output

Print the only integer c — the number of parallelograms with the vertices at the given points.

Sample Input

Input
40 11 01 12 0
Output
1

Source

Educational Codeforces Round 11

给出n个点的坐标,问你这些点可以组成多少个平行四边形,数据很大,判断平行边肯定是不可以的,会超时,平行四边形有一个性质,对角线互相平分,我们可以得到所有线段的中点,然后判断这些中点重合的有多少,一个中点代表两个给定点,所以每一对中点就可以表示一个平行四边形,判重的时候只需要一个for不会超时

#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;struct node{double x,y;}p[2010],q[4000000+10];int cmp(node s1,node s2){if(s1.x==s2.x)return s1.y<s2.y;return s1.x<s2.x;}int main(){int n;while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);sort(p,p+n,cmp);int cnt=0;for(int i=0;i<n;i++){for(int j=i+1;j<n;j++){q[cnt].x=(p[i].x+p[j].x)/2;q[cnt++].y=(p[i].y+p[j].y)/2;}}sort(q,q+cnt,cmp);__int64 sum=0,cout=0;node s=q[0];for(int i=0;i<cnt;i++){if(abs(s.x-q[i].x)<1e-6&&abs(s.y-q[i].y)<1e-6)cout++;else{if(cout>=1)sum+=cout*(cout-1)/2;cout=1;s=q[i];}}printf("%I64d\n",sum);}return 0;}


0 0
原创粉丝点击