10319 How Many Parallelograms
时间限制:1000MS 内存限制:65535K
提交次数:0 通过次数:0
题型: 编程题 语言: G++;GCC
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. You may assume that No four points are in a straight line.
输入格式
The first line 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 the number of the parallelograms as described above.
输入样例
7 -2 -1 8 9 5 7 1 1 4 8 2 0 9 8
输出样例
6
题意:给出n个点,然后在n个点内找出可以组成多少个平行四边形。
利用平行四边形判定定理:对角线平分,那么两对角线的中点是相同的,利用这个定理,就可以去暴力了,数据量1000,那么最多有100w个中点,然后在100w个中点里面找出两两相同的中点,那么就有1个平行四边形,但是这个O(n2)直接TLE了,所有要用快速排序去优化一下,当x1!=x2的时候那么它与之后的点就一定不会相同,思路就是这样了,勉强ac了把700+ms
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string.h>
using namespace std;
struct node
{
long long x,y;
} a[1009],mid[1000009];
int cmp(node a,node b)
{
return a.x<b.x;
}
int main()
{
int n,i,j;
while(~scanf("%d",&n))
{
for(i=1; i<=n; i++)
{
scanf("%lld%lld",&a[i].x,&a[i].y);
}
int t=0;
for(i=1; i<=n; i++)
{
for(j=i+1; j<=n; j++)
{
mid[t].x=a[i].x+a[j].x;
mid[t].y=a[i].y+a[j].y;//任意两点的中点值,因为/2会有小数,直接不/2
t++;
}
}
sort(mid,mid+t,cmp);
int count=0;
for(i=0; i<=t-1; i++)
{
for(j=i+1; j<=t-1; j++)
{
if(mid[i].x!=mid[j].x) break;
if(mid[i].x==mid[j].x&&mid[i].y==mid[j].y) count++;
}
}
printf("%d\n",count);
}
}