【CodeForces 660D】Number of Parallelograms
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.
4
0 1
1 0
1 1
2 0
1
给你n个点的坐标,问这n个点能形成多少个不相同的平行四边形?
根据平行四边形的性质,两条对角线相交于一点,所以可以将这n个点两两连接并找出它们的中点并记录下来。
然后根据这些中点来判断有多少个平行四边形,因为只要有两个中点重合,那么它们两条线就可以组成一个平行四边形。
所以先找出重合中点的个数s,那么它们可以组成的平行四边形个数为C(s,2)。
将它们求和即为所求。
#include<bits/stdc++.h> using namespace std; struct p { int x,y; }a[2001]; int com(int n,int m) { int i,s=1; for(i=1;i<=m;i++) s=s*(n+1-i)/i; return s; } int main() { int n,i,j,k,s=0; map<pair<int,int>,int>ma; map<pair<int,int>,int>::iterator it; cin>>n; for(i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y); for(i=1;i<=n-1;i++) for(j=i+1;j<=n;j++) ma[make_pair(a[i].x+a[j].x,a[i].y+a[j].y)]++; for(it=ma.begin();it!=ma.end();it++) s=s+com(it->second,2); printf("%d\n",s); return 0; }
上一篇: 每天游泳能减肥吗 怎么样游泳能减肥
下一篇: 29.C++- 异常处理
推荐阅读
-
【CodeForces 660D】Number of Parallelograms
-
Number With The Given Amount Of Divisors CodeForces - 27E(反素数相关)
-
Codeforces Round #651 (Div. 2) C. Number Game
-
CodeForces - 1426F Number of Subsequences(dp)
-
Codeforces Round #209 (Div. 2) C. Prime Number_html/css_WEB-ITnose
-
Codeforces Round #411 (Div. 2)D. Minimum number of steps(贪心)
-
【CodeForces 660D】Number of Parallelograms
-
Codeforces Round #209 (Div. 2) C. Prime Number_html/css_WEB-ITnose
-
Codeforces Round #480 (Div. 2) E. The Number Games(倍增+贪心)
-
CodeForces - 1426F Number of Subsequences(dp)