1058 - Parallelogram Counting 计算几何

1058 - Parallelogram Counting

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

Input starts with an integer T (≤ 15), denoting the number of test cases.

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

For each case, print the case number and the number of parallelograms that can be formed.

Sample Input

Output for 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

Case 1: 5

Case 2: 6

 

 

 

分析:学了这么多年数学,然而只知道平行四边形对角线交于一点,却没想到只要存在两点的中点与另两点的中点相同,就能构成平行四边形。

代码:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
#define N 510000

struct node
{
int x;
int y;
}arr[N], mid[N];
bool cmp(struct node a, struct node b)
{
if(a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
int main(void)
{
int T, cas, n;

scanf("%d", &T);
cas = 0;

while(T--)
{
cas++;
memset(mid, 0, sizeof(mid));

scanf("%d", &n);

for(int i = 0; i < n; i++)
{
scanf("%d%d", &arr[i].x, &arr[i].y);
}
int num = 0;

for(int i = 0; i < n-1; i++)
{
for(int j = i+1; j < n; j++)
{
mid[num].x = arr[i].x + arr[j].x;
mid[num].y = arr[i].y + arr[j].y;
num++;
}
}
sort(mid, mid+num, cmp);/// 排序是为了方便下面比较
int cnt = 1;
int ans = 0;
int flag = 0;

for(int i = 1; i < num; i++)
{
if(mid[i].x == mid[flag].x && mid[i].y == mid[flag].y)
{
cnt++;
}
else
{
ans += cnt * (cnt - 1) / 2;///计算就是组合数:从 n个数里面取出 2个数 ,就是 C(n,2)
cnt = 1;
flag = i;
}
}
if(cnt>1)
ans += (cnt - 1) * cnt / 2; /// 判断循环的最后一组数据,如果也存在相同的点,就加上
printf("Case %d: %d\n", cas, ans);
}
return 0;
}

posted on 2017-07-14 10:37  缄默。  阅读(135)  评论(0编辑  收藏  举报

导航