您的位置:首页 > 其它

Lightoj 1058 (组合数学)

2016-08-13 21:53 423 查看
Description

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

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

Sample Output

Case 1: 5

Case 2: 6

还是得窃取别人的智慧

对于一个平行四边形啦,它们对角线的中点相交诶,所以说,只需要判断一下是否存在两条线中点相同就好了,然后结果就所以取两条线就过了

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;
#define M 1010
#define LL long long

bool vis[M][M];
struct node
{
double x, y;
};
node no[M*M/2];

bool cmp(node a, node b)
{
if(a.x == b.x)
{
return a.y<b.y;
}
return a.x < b.x;
}

int main()
{
int t, n;
double x[M], y[M];
scanf("%d", &t);
for(int ca=1; ca<=t; ca++)
{
scanf("%d", &n);
for(int i=0; i<n; i++)
{
scanf("%lf%lf", &x[i], &y[i]);
}
int k = 0;
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
no[k].x = x[i] + x[j];
no[k++].y = y[i] + y[j];
}
}
sort(no, no+k, cmp);//方便找出相同的点诶
int flag = 0, num = 1, ans = 0;
for(int i=1; i<k; i++)
{
if(no[i].x == no[flag].x && no[i].y == no[flag].y)
{
num++;
}
else
{
ans += num * (num-1) / 2;
num = 1;
flag = i;
}
}

printf("Case %d: %d\n", ca, ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: