CodeForces 660D Number of Parallelograms

枚举两点,确定一条线段,计算每条线段的中点坐标。

按线段中点坐标排个序。找出每一种坐标有几个。

假设第x种坐标有y个,那么这些线段可以组成y*(y-1)/2种平行四边形。

累加即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;

const int maxn=2000+10;
long long x[maxn],y[maxn];
int n;
struct X
{
    long long a,b;
}s[maxn*maxn];

bool cmp(const X&a,const X&b)
{
    if(a.a==b.a) return a.b<b.b;
    return a.a<b.a;
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lld%lld",&x[i],&y[i]);
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            s[cnt].a=x[i]+x[j];
            s[cnt].b=y[i]+y[j];
            cnt++;
        }
    }
    sort(s,s+cnt,cmp);
    long long num=1;
    long long ans=0;
    for(int i=1;i<cnt;i++)
    {
        if(s[i].a==s[i-1].a&&s[i].b==s[i-1].b) num++;
        else
        {
            ans=ans+num*(num-1)/2;
            num=1;
        }
    }
    printf("%lld
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/zufezzt/p/5427838.html