scau 10319 How Many Parallelograms

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);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值